blob: 15eaaaeab720c6bc2206a1354226592a8824c749 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * message.c: functions for displaying messages on the command line
12 */
13
Bram Moolenaar85a20022019-12-21 18:25:54 +010014#define MESSAGE_FILE // don't include prototype for smsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015
16#include "vim.h"
17
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010018static void add_msg_hist(char_u *s, int len, int attr);
19static void hit_return_msg(void);
20static void msg_home_replace_attr(char_u *fname, int attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +010021static void msg_puts_attr_len(char *str, int maxlen, int attr);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static void msg_puts_display(char_u *str, int maxlen, int attr, int recurse);
23static void msg_scroll_up(void);
24static void inc_msg_scrolled(void);
25static void store_sb_text(char_u **sb_str, char_u *s, int attr, int *sb_col, int finish);
26static void t_puts(int *t_col, char_u *t_s, char_u *s, int attr);
27static void msg_puts_printf(char_u *str, int maxlen);
28static int do_more_prompt(int typed_char);
29static void msg_screen_putchar(int c, int attr);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020030static void msg_moremsg(int full);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010031static int msg_check_screen(void);
32static void redir_write(char_u *s, int maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#ifdef FEAT_CON_DIALOG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton);
Bram Moolenaar85a20022019-12-21 18:25:54 +010035static int confirm_msg_used = FALSE; // displaying confirm_msg
36static char_u *confirm_msg = NULL; // ":confirm" message
37static char_u *confirm_msg_tail; // tail of confirm_msg
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020038static void display_confirm_msg(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#endif
Bram Moolenaar3b8c7082022-11-30 20:20:56 +000040#ifdef FEAT_EVAL
Bram Moolenaar958dc692016-12-01 15:34:12 +010041static int emsg_to_channel_log = FALSE;
42#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000043
44struct msg_hist
45{
46 struct msg_hist *next;
47 char_u *msg;
48 int attr;
49};
50
51static struct msg_hist *first_msg_hist = NULL;
52static struct msg_hist *last_msg_hist = NULL;
53static int msg_hist_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000054
Bram Moolenaarb5b5b892011-09-14 15:39:29 +020055static FILE *verbose_fd = NULL;
56static int verbose_did_open = FALSE;
57
Christian Brabandt45e07042024-11-11 20:52:55 +010058static int did_warn_clipboard = FALSE;
Christian Brabandt45e07042024-11-11 20:52:55 +010059
Bram Moolenaar071d4272004-06-13 20:20:40 +000060/*
61 * When writing messages to the screen, there are many different situations.
62 * A number of variables is used to remember the current state:
63 * msg_didany TRUE when messages were written since the last time the
64 * user reacted to a prompt.
65 * Reset: After hitting a key for the hit-return prompt,
66 * hitting <CR> for the command line or input().
67 * Set: When any message is written to the screen.
68 * msg_didout TRUE when something was written to the current line.
69 * Reset: When advancing to the next line, when the current
70 * text can be overwritten.
71 * Set: When any message is written to the screen.
72 * msg_nowait No extra delay for the last drawn message.
73 * Used in normal_cmd() before the mode message is drawn.
74 * emsg_on_display There was an error message recently. Indicates that there
75 * should be a delay before redrawing.
76 * msg_scroll The next message should not overwrite the current one.
77 * msg_scrolled How many lines the screen has been scrolled (because of
78 * messages). Used in update_screen() to scroll the screen
79 * back. Incremented each time the screen scrolls a line.
80 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
81 * writes something without scrolling should not make
82 * need_wait_return to be set. This is a hack to make ":ts"
83 * work without an extra prompt.
84 * lines_left Number of lines available for messages before the
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +010085 * more-prompt is to be given. -1 when not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 * need_wait_return TRUE when the hit-return prompt is needed.
87 * Reset: After giving the hit-return prompt, when the user
88 * has answered some other prompt.
89 * Set: When the ruler or typeahead display is overwritten,
90 * scrolling the screen for some message.
91 * keep_msg Message to be displayed after redrawing the screen, in
92 * main_loop().
93 * This is an allocated string or NULL when not used.
94 */
95
96/*
97 * msg(s) - displays the string 's' on the status line
98 * When terminal not initialized (yet) mch_errmsg(..) is used.
Bram Moolenaar13608d82022-08-29 15:06:50 +010099 * return TRUE if wait_return() not called
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100 */
101 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100102msg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103{
104 return msg_attr_keep(s, 0, FALSE);
105}
106
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000107/*
108 * Like msg() but keep it silent when 'verbosefile' is set.
109 */
110 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100111verb_msg(char *s)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000112{
113 int n;
114
115 verbose_enter();
116 n = msg_attr_keep(s, 0, FALSE);
117 verbose_leave();
118
119 return n;
120}
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000121
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100123msg_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124{
125 return msg_attr_keep(s, attr, FALSE);
126}
127
128 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100129msg_attr_keep(
Bram Moolenaar32526b32019-01-19 17:43:09 +0100130 char *s,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100131 int attr,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100132 int keep) // TRUE: set keep_msg if it doesn't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133{
134 static int entered = 0;
135 int retval;
136 char_u *buf = NULL;
137
Bram Moolenaar85a20022019-12-21 18:25:54 +0100138 // Skip messages not matching ":filter pattern".
139 // Don't filter when there is an error.
Bram Moolenaar32526b32019-01-19 17:43:09 +0100140 if (!emsg_on_display && message_filtered((char_u *)s))
Bram Moolenaar7b668e82016-08-23 23:51:21 +0200141 return TRUE;
142
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143#ifdef FEAT_EVAL
144 if (attr == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100145 set_vim_var_string(VV_STATUSMSG, (char_u *)s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146#endif
147
148 /*
149 * It is possible that displaying a messages causes a problem (e.g.,
150 * when redrawing the window), which causes another message, etc.. To
151 * break this loop, limit the recursiveness to 3 levels.
152 */
153 if (entered >= 3)
154 return TRUE;
155 ++entered;
156
Bram Moolenaar85a20022019-12-21 18:25:54 +0100157 // Add message to history (unless it's a repeated kept message or a
158 // truncated message)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100159 if ((char_u *)s != keep_msg
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 || (*s != '<'
161 && last_msg_hist != NULL
162 && last_msg_hist->msg != NULL
163 && STRCMP(s, last_msg_hist->msg)))
Bram Moolenaar32526b32019-01-19 17:43:09 +0100164 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165
Bram Moolenaar4c5678f2022-11-30 18:12:19 +0000166#ifdef FEAT_EVAL
Bram Moolenaar958dc692016-12-01 15:34:12 +0100167 if (emsg_to_channel_log)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100168 // Write message in the channel log.
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000169 ch_log(NULL, "ERROR: %s", s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100170#endif
171
Bram Moolenaar85a20022019-12-21 18:25:54 +0100172 // Truncate the message if needed.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000173 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +0100174 buf = msg_strtrunc((char_u *)s, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 if (buf != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100176 s = (char *)buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177
Bram Moolenaar32526b32019-01-19 17:43:09 +0100178 msg_outtrans_attr((char_u *)s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 msg_clr_eos();
180 retval = msg_end();
181
Bram Moolenaar32526b32019-01-19 17:43:09 +0100182 if (keep && retval && vim_strsize((char_u *)s)
183 < (int)(Rows - cmdline_row - 1) * Columns + sc_col)
184 set_keep_msg((char_u *)s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185
Rob Pilling726f7f92022-01-20 14:44:38 +0000186 need_fileinfo = FALSE;
187
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 vim_free(buf);
189 --entered;
190 return retval;
191}
192
193/*
194 * Truncate a string such that it can be printed without causing a scroll.
195 * Returns an allocated string or NULL when no truncating is done.
196 */
197 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100198msg_strtrunc(
199 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100200 int force) // always truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201{
202 char_u *buf = NULL;
203 int len;
204 int room;
205
Bram Moolenaar85a20022019-12-21 18:25:54 +0100206 // May truncate message to avoid a hit-return prompt
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000207 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
208 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 {
210 len = vim_strsize(s);
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +0100211 if (msg_scrolled != 0
212#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +0100213 || in_echowindow
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +0100214#endif
215 )
Bram Moolenaar85a20022019-12-21 18:25:54 +0100216 // Use all the columns.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000217 room = (int)(Rows - msg_row) * Columns - 1;
218 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100219 // Use up to 'showcmd' column.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000220 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 if (len > room && room > 0)
222 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100224 // may have up to 18 bytes per cell (6 per char, up to two
225 // composing chars)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100226 len = (room + 2) * 18;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 else if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100228 // may have up to 2 bytes per cell for euc-jp
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100229 len = (room + 2) * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 else
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100231 len = room + 2;
232 buf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 if (buf != NULL)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100234 trunc_string(s, buf, room, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 }
236 }
237 return buf;
238}
239
240/*
241 * Truncate a string "s" to "buf" with cell width "room".
242 * "s" and "buf" may be equal.
243 */
244 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100245trunc_string(
246 char_u *s,
247 char_u *buf,
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200248 int room_in,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100249 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100251 size_t room = room_in - 3; // "..." takes 3 chars
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200252 size_t half;
253 size_t len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 int e;
255 int i;
256 int n;
257
Bram Moolenaar62818152021-02-14 15:37:30 +0100258 if (*s == NUL)
259 {
260 if (buflen > 0)
261 *buf = NUL;
262 return;
263 }
264
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200265 if (room_in < 3)
266 room = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 half = room / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268
Bram Moolenaar85a20022019-12-21 18:25:54 +0100269 // First part: Start of the string.
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100270 for (e = 0; len < half && e < buflen; ++e)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 {
272 if (s[e] == NUL)
273 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100274 // text fits without truncating!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 buf[e] = NUL;
276 return;
277 }
278 n = ptr2cells(s + e);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200279 if (len + n > half)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 break;
281 len += n;
282 buf[e] = s[e];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000284 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100286 if (++e == buflen)
287 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 buf[e] = s[e];
289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 }
291
Bram Moolenaar85a20022019-12-21 18:25:54 +0100292 // Last part: End of the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 i = e;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 if (enc_dbcs != 0)
295 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100296 // For DBCS going backwards in a string is slow, but
297 // computing the cell width isn't too slow: go forward
298 // until the rest fits.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 n = vim_strsize(s + i);
300 while (len + n > room)
301 {
302 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000303 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 }
305 }
306 else if (enc_utf8)
307 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100308 // For UTF-8 we can go backwards easily.
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000309 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 for (;;)
311 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000312 do
Bram Moolenaarace95982017-03-29 17:30:27 +0200313 half = half - utf_head_off(s, s + half - 1) - 1;
Bram Moolenaarb9644432016-07-19 12:33:44 +0200314 while (half > 0 && utf_iscomposing(utf_ptr2char(s + half)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 n = ptr2cells(s + half);
Bram Moolenaarb9644432016-07-19 12:33:44 +0200316 if (len + n > room || half == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 break;
318 len += n;
Bram Moolenaara5c0cc12016-07-30 16:40:39 +0200319 i = (int)half;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 }
321 }
322 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 {
Bram Moolenaard0337e32019-12-30 17:55:34 +0100324 for (i = (int)STRLEN(s);
325 i - 1 >= 0 && len + (n = ptr2cells(s + i - 1)) <= room; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 len += n;
327 }
328
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200329
330 if (i <= e + 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100331 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100332 // text fits without truncating
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200333 if (s != buf)
334 {
335 len = STRLEN(s);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200336 if (len >= (size_t)buflen)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200337 len = buflen - 1;
338 len = len - e + 1;
339 if (len < 1)
340 buf[e - 1] = NUL;
341 else
342 mch_memmove(buf + e, s + e, len);
343 }
344 }
345 else if (e + 3 < buflen)
346 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100347 // set the middle and copy the last part
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100348 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200349 len = STRLEN(s + i) + 1;
350 if (len >= (size_t)buflen - e - 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100351 len = buflen - e - 3 - 1;
352 mch_memmove(buf + e + 3, s + i, len);
353 buf[e + 3 + len - 1] = NUL;
354 }
355 else
356 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100357 // can't fit in the "...", just truncate it
Christian Brabandt3bd7fa12023-10-02 20:59:08 +0200358 buf[buflen - 1] = NUL;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360}
361
362/*
363 * Automatic prototype generation does not understand this function.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100364 * Note: Caller of smsg() and smsg_attr() must check the resulting string is
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 * shorter than IOSIZE!!!
366 */
367#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100369int vim_snprintf(char *str, size_t str_m, const char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000370
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100372smsg(const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200374 if (IObuff == NULL)
375 {
376 // Very early in initialisation and already something wrong, just
377 // give the raw message so the user at least gets a hint.
378 return msg((char *)s);
379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000381 va_list arglist;
382
383 va_start(arglist, s);
384 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
385 va_end(arglist);
386 return msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387}
388
389 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100390smsg_attr(int attr, const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200392 if (IObuff == NULL)
393 {
394 // Very early in initialisation and already something wrong, just
395 // give the raw message so the user at least gets a hint.
396 return msg_attr((char *)s, attr);
397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000399 va_list arglist;
400
401 va_start(arglist, s);
402 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
403 va_end(arglist);
404 return msg_attr((char *)IObuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405}
406
Bram Moolenaare0429682018-07-01 16:44:03 +0200407 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100408smsg_attr_keep(int attr, const char *s, ...)
Bram Moolenaare0429682018-07-01 16:44:03 +0200409{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200410 if (IObuff == NULL)
411 {
412 // Very early in initialisation and already something wrong, just
413 // give the raw message so the user at least gets a hint.
414 return msg_attr_keep((char *)s, attr, TRUE);
415 }
Bram Moolenaare0429682018-07-01 16:44:03 +0200416
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000417 va_list arglist;
418
419 va_start(arglist, s);
420 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
421 va_end(arglist);
422 return msg_attr_keep((char *)IObuff, attr, TRUE);
Bram Moolenaare0429682018-07-01 16:44:03 +0200423}
424
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425#endif
426
427/*
428 * Remember the last sourcing name/lnum used in an error message, so that it
429 * isn't printed each time when it didn't change.
430 */
431static int last_sourcing_lnum = 0;
432static char_u *last_sourcing_name = NULL;
433
434/*
435 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
436 * for the next error message;
437 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000438 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100439reset_last_sourcing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100441 VIM_CLEAR(last_sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 last_sourcing_lnum = 0;
443}
444
445/*
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100446 * Return TRUE if "SOURCING_NAME" differs from "last_sourcing_name".
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000447 */
448 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100449other_sourcing_name(void)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000450{
Bram Moolenaar0bd8d052021-11-25 13:39:28 +0000451 if (HAVE_SOURCING_INFO && SOURCING_NAME != NULL)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000452 {
453 if (last_sourcing_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100454 return STRCMP(SOURCING_NAME, last_sourcing_name) != 0;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000455 return TRUE;
456 }
457 return FALSE;
458}
459
460/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 * Get the message about the source, as used for an error message.
462 * Returns an allocated string with room for one more character.
463 * Returns NULL when no message is to be given.
464 */
465 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100466get_emsg_source(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467{
468 char_u *Buf, *p;
469
Bram Moolenaar0bd8d052021-11-25 13:39:28 +0000470 if (HAVE_SOURCING_INFO && SOURCING_NAME != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 {
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200472 char_u *sname = estack_sfile(ESTACK_NONE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100473 char_u *tofree = sname;
474
475 if (sname == NULL)
476 sname = SOURCING_NAME;
477
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +0200478#ifdef FEAT_EVAL
479 if (estack_compiling)
480 p = (char_u *)_("Error detected while compiling %s:");
481 else
482#endif
483 p = (char_u *)_("Error detected while processing %s:");
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100484 Buf = alloc(STRLEN(sname) + STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100486 sprintf((char *)Buf, (char *)p, sname);
487 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 return Buf;
489 }
490 return NULL;
491}
492
493/*
494 * Get the message about the source lnum, as used for an error message.
495 * Returns an allocated string with room for one more character.
496 * Returns NULL when no message is to be given.
497 */
498 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100499get_emsg_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500{
501 char_u *Buf, *p;
502
Bram Moolenaar85a20022019-12-21 18:25:54 +0100503 // lnum is 0 when executing a command from the command line
504 // argument, we don't want a line number then
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100505 if (SOURCING_NAME != NULL
506 && (other_sourcing_name() || SOURCING_LNUM != last_sourcing_lnum)
507 && SOURCING_LNUM != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 {
509 p = (char_u *)_("line %4ld:");
Bram Moolenaar964b3742019-05-24 18:54:09 +0200510 Buf = alloc(STRLEN(p) + 20);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100512 sprintf((char *)Buf, (char *)p, (long)SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 return Buf;
514 }
515 return NULL;
516}
517
518/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000519 * Display name and line number for the source of an error.
520 * Remember the file name and line number, so that for the next error the info
521 * is only displayed if it changed.
522 */
523 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100524msg_source(int attr)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000525{
526 char_u *p;
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000527 static int recursive = FALSE;
528
529 // Bail out if something called here causes an error.
530 if (recursive)
531 return;
532 recursive = TRUE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000533
534 ++no_wait_return;
535 p = get_emsg_source();
536 if (p != NULL)
537 {
zeertzjqbdedd2b2022-09-20 12:45:15 +0100538 msg_scroll = TRUE; // this will take more than one line
Bram Moolenaar32526b32019-01-19 17:43:09 +0100539 msg_attr((char *)p, attr);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000540 vim_free(p);
541 }
542 p = get_emsg_lnum();
543 if (p != NULL)
544 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100545 msg_attr((char *)p, HL_ATTR(HLF_N));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000546 vim_free(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100547 last_sourcing_lnum = SOURCING_LNUM; // only once for each line
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000548 }
549
Bram Moolenaar85a20022019-12-21 18:25:54 +0100550 // remember the last sourcing name printed, also when it's empty
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100551 if (SOURCING_NAME == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000552 {
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000553 VIM_CLEAR(last_sourcing_name);
554 if (SOURCING_NAME != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100555 last_sourcing_name = vim_strsave(SOURCING_NAME);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000556 }
557 --no_wait_return;
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000558
559 recursive = FALSE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000560}
561
562/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000563 * Return TRUE if not giving error messages right now:
564 * If "emsg_off" is set: no error messages at the moment.
565 * If "msg" is in 'debug': do error message but without side effects.
566 * If "emsg_skip" is set: never do error messages.
567 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200568 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100569emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000570{
571 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
572 && vim_strchr(p_debug, 't') == NULL)
573#ifdef FEAT_EVAL
574 || emsg_skip > 0
575#endif
576 )
577 return TRUE;
578 return FALSE;
579}
580
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +0100581#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100582static garray_T ignore_error_list = GA_EMPTY;
583
584 void
585ignore_error_for_testing(char_u *error)
586{
587 if (ignore_error_list.ga_itemsize == 0)
588 ga_init2(&ignore_error_list, sizeof(char_u *), 1);
589
Bram Moolenaar461a7fc2018-12-22 13:28:07 +0100590 if (STRCMP("RESET", error) == 0)
591 ga_clear_strings(&ignore_error_list);
592 else
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000593 ga_copy_string(&ignore_error_list, error);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100594}
595
596 static int
Bram Moolenaar097c5372023-05-24 21:02:24 +0100597ignore_error(const char *msg)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100598{
599 int i;
600
601 for (i = 0; i < ignore_error_list.ga_len; ++i)
Bram Moolenaar097c5372023-05-24 21:02:24 +0100602 if (strstr(msg,
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100603 (char *)((char_u **)(ignore_error_list.ga_data))[i]) != NULL)
604 return TRUE;
605 return FALSE;
606}
607#endif
608
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200609#if !defined(HAVE_STRERROR) || defined(PROTO)
610/*
611 * Replacement for perror() that behaves more or less like emsg() was called.
Bram Moolenaar53989552019-12-23 22:59:18 +0100612 * v:errmsg will be set and called_emsg will be incremented.
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200613 */
614 void
615do_perror(char *msg)
616{
617 perror(msg);
618 ++emsg_silent;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100619 emsg(msg);
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200620 --emsg_silent;
621}
622#endif
623
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000624/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100625 * emsg_core() - display an error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 *
627 * Rings the bell, if appropriate, and calls message() to do the real work
628 * When terminal not initialized (yet) mch_errmsg(..) is used.
629 *
Bram Moolenaar13608d82022-08-29 15:06:50 +0100630 * Return TRUE if wait_return() not called.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100631 * Note: caller must check 'emsg_not_now()' before calling this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100633 static int
Bram Moolenaar097c5372023-05-24 21:02:24 +0100634emsg_core(const char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635{
636 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 char_u *p;
Bram Moolenaar958dc692016-12-01 15:34:12 +0100638 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639#ifdef FEAT_EVAL
640 int ignore = FALSE;
641 int severe;
642#endif
643
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100644#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100645 // When testing some errors are turned into a normal message.
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100646 if (ignore_error(s))
Bram Moolenaar85a20022019-12-21 18:25:54 +0100647 // don't call msg() if it results in a dialog
Bram Moolenaar32526b32019-01-19 17:43:09 +0100648 return msg_use_printf() ? FALSE : msg((char *)s);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100649#endif
650
Bram Moolenaar53989552019-12-23 22:59:18 +0100651 ++called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100654 // If "emsg_severe" is TRUE: When an error exception is to be thrown,
655 // prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 severe = emsg_severe;
657 emsg_severe = FALSE;
658#endif
659
Bram Moolenaar57657d82006-04-21 22:12:41 +0000660 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 {
662#ifdef FEAT_EVAL
663 /*
664 * Cause a throw of an error exception if appropriate. Don't display
665 * the error message in this case. (If no matching catch clause will
666 * be found, the message will be displayed later on.) "ignore" is set
667 * when the message should be ignored completely (used for the
668 * interrupt message).
669 */
Bram Moolenaar097c5372023-05-24 21:02:24 +0100670 if (cause_errthrow((char_u *)s, severe, &ignore) == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 {
672 if (!ignore)
Bram Moolenaar76a63452018-11-28 20:38:37 +0100673 ++did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 return TRUE;
675 }
676
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100677 if (in_assert_fails && emsg_assert_fails_msg == NULL)
Bram Moolenaar1d634542020-08-18 13:41:50 +0200678 {
Bram Moolenaar097c5372023-05-24 21:02:24 +0100679 emsg_assert_fails_msg = vim_strsave((char_u *)s);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200680 emsg_assert_fails_lnum = SOURCING_LNUM;
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200681 vim_free(emsg_assert_fails_context);
682 emsg_assert_fails_context = vim_strsave(
683 SOURCING_NAME == NULL ? (char_u *)"" : SOURCING_NAME);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200684 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200685
Bram Moolenaar85a20022019-12-21 18:25:54 +0100686 // set "v:errmsg", also when using ":silent! cmd"
Bram Moolenaar097c5372023-05-24 21:02:24 +0100687 set_vim_var_string(VV_ERRMSG, (char_u *)s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688#endif
689
690 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000691 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 * But do write it to the redirection file.
693 */
694 if (emsg_silent != 0)
695 {
Bram Moolenaar599410c2021-04-10 14:03:43 +0200696#ifdef FEAT_EVAL
697 ++did_emsg_silent;
698#endif
Bram Moolenaar79815f12016-07-09 17:07:29 +0200699 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200701 msg_start();
702 p = get_emsg_source();
703 if (p != NULL)
704 {
705 STRCAT(p, "\n");
706 redir_write(p, -1);
707 vim_free(p);
708 }
709 p = get_emsg_lnum();
710 if (p != NULL)
711 {
712 STRCAT(p, "\n");
713 redir_write(p, -1);
714 vim_free(p);
715 }
Bram Moolenaar097c5372023-05-24 21:02:24 +0100716 redir_write((char_u *)s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +0100718#ifdef FEAT_EVAL
719 // Only increment did_emsg_def when :silent! wasn't used inside the
720 // :def function.
721 if (emsg_silent == emsg_silent_def)
722 ++did_emsg_def;
723#endif
Bram Moolenaar4c5678f2022-11-30 18:12:19 +0000724#ifdef FEAT_EVAL
Bram Moolenaar097c5372023-05-24 21:02:24 +0100725 ch_log(NULL, "ERROR silent: %s", s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 return TRUE;
728 }
729
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100730 ex_exitval = 1;
731
Bram Moolenaar85a20022019-12-21 18:25:54 +0100732 // Reset msg_silent, an error causes messages to be switched back on.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 msg_silent = 0;
734 cmd_silent = FALSE;
735
Bram Moolenaar85a20022019-12-21 18:25:54 +0100736 if (global_busy) // break :global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 ++global_busy;
738
739 if (p_eb)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100740 beep_flush(); // also includes flush_buffers()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 else
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200742 flush_buffers(FLUSH_MINIMAL); // flush internal buffers
Bram Moolenaar76a63452018-11-28 20:38:37 +0100743 ++did_emsg; // flag for DoOneCmd()
Bram Moolenaare723c422017-09-06 23:40:10 +0200744#ifdef FEAT_EVAL
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200745 ++uncaught_emsg;
Bram Moolenaare723c422017-09-06 23:40:10 +0200746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 }
748
Bram Moolenaar878e1d22022-08-28 17:53:23 +0100749#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +0100750 if (!in_echowindow)
Bram Moolenaar878e1d22022-08-28 17:53:23 +0100751#endif
752 emsg_on_display = TRUE; // remember there is an error message
753
754 attr = HL_ATTR(HLF_E); // set highlight mode for error messages
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000755 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100756 need_wait_return = TRUE; // needed in case emsg() is called after
Bram Moolenaar13608d82022-08-29 15:06:50 +0100757 // wait_return() has reset need_wait_return
Bram Moolenaar85a20022019-12-21 18:25:54 +0100758 // and a redraw is expected because
759 // msg_scrolled is non-zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760
Bram Moolenaar958dc692016-12-01 15:34:12 +0100761#ifdef FEAT_JOB_CHANNEL
762 emsg_to_channel_log = TRUE;
763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 /*
765 * Display name and line number for the source of the error.
766 */
zeertzjqbdedd2b2022-09-20 12:45:15 +0100767 msg_scroll = TRUE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000768 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769
770 /*
771 * Display the error message itself.
772 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100773 msg_nowait = FALSE; // wait for this msg
Bram Moolenaar32526b32019-01-19 17:43:09 +0100774 r = msg_attr((char *)s, attr);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100775
776#ifdef FEAT_JOB_CHANNEL
777 emsg_to_channel_log = FALSE;
778#endif
779 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780}
781
782/*
Bram Moolenaar097c5372023-05-24 21:02:24 +0100783 * Print error message "s". Should already be translated.
784 * Return TRUE if wait_return() not called.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 */
786 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100787emsg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100789 // Skip this if not giving error messages at the moment.
Bram Moolenaar097c5372023-05-24 21:02:24 +0100790 if (emsg_not_now())
791 return TRUE;
792
793 return emsg_core(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794}
795
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100796#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100797/*
Bram Moolenaar097c5372023-05-24 21:02:24 +0100798 * Print error message "s" with format string and variable arguments.
799 * "s" should already be translated.
800 * Note: caller must not use "IObuff" for "s"!
801 * Return TRUE if wait_return() not called.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100802 */
803 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100804semsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100805{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200806 // Skip this if not giving error messages at the moment.
Bram Moolenaar097c5372023-05-24 21:02:24 +0100807 if (emsg_not_now())
808 return TRUE;
Bram Moolenaar95f09602016-11-10 20:01:45 +0100809
Bram Moolenaar097c5372023-05-24 21:02:24 +0100810 if (IObuff == NULL)
811 // Very early in initialisation and already something wrong, just
812 // give the raw message so the user at least gets a hint.
813 return emsg_core(s);
814
815 va_list ap;
816
817 va_start(ap, s);
818 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
819 va_end(ap);
820 return emsg_core((char *)IObuff);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100821}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100822#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100823
824/*
825 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
826 * defined. It is used for internal errors only, so that they can be
827 * detected when fuzzing vim.
828 */
829 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100830iemsg(char *s)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100831{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000832 if (emsg_not_now())
833 return;
834
Bram Moolenaar097c5372023-05-24 21:02:24 +0100835 // Give a generic error which is translated. The error itself may not be
836 // translated, it almost never shows.
837 emsg_core(_(e_internal_error_please_report_a_bug));
838
839 emsg_core(s);
Bram Moolenaar096ca732022-01-01 00:55:28 +0000840#if defined(ABORT_ON_INTERNAL_ERROR) && defined(FEAT_EVAL)
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000841 set_vim_var_string(VV_ERRMSG, (char_u *)s, -1);
842 msg_putchar('\n'); // avoid overwriting the error message
843 out_flush();
844 abort();
Bram Moolenaar95f09602016-11-10 20:01:45 +0100845#endif
846}
847
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100848#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100849/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100850 * Same as semsg(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
Bram Moolenaar95f09602016-11-10 20:01:45 +0100851 * defined. It is used for internal errors only, so that they can be
852 * detected when fuzzing vim.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100853 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100854 */
855 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100856siemsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100857{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000858 if (emsg_not_now())
859 return;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100860
Bram Moolenaar097c5372023-05-24 21:02:24 +0100861 // Give a generic error which is translated. The error itself may not be
862 // translated, it almost never shows.
863 emsg_core(_(e_internal_error_please_report_a_bug));
864
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000865 if (IObuff == NULL)
866 {
867 // Very early in initialisation and already something wrong, just
868 // give the raw message so the user at least gets a hint.
Bram Moolenaar097c5372023-05-24 21:02:24 +0100869 emsg_core(s);
Bram Moolenaar213e70e2022-08-19 13:17:21 +0100870 }
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000871 else
872 {
873 va_list ap;
874
875 va_start(ap, s);
876 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
877 va_end(ap);
Bram Moolenaar097c5372023-05-24 21:02:24 +0100878 emsg_core((char *)IObuff);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000879 }
880# ifdef ABORT_ON_INTERNAL_ERROR
881 msg_putchar('\n'); // avoid overwriting the error message
882 out_flush();
883 abort();
884# endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100885}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100886#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100887
888/*
889 * Give an "Internal error" message.
890 */
891 void
892internal_error(char *where)
893{
Bram Moolenaar097c5372023-05-24 21:02:24 +0100894 // Give a generic error which is translated. The error itself may not be
895 // translated, it almost never shows.
896 emsg_core(_(e_internal_error_please_report_a_bug));
897
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000898 siemsg(_(e_internal_error_str), where);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100899}
900
Dominique Pelle748b3082022-01-08 12:41:16 +0000901#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardd589232020-02-29 17:38:12 +0100902/*
903 * Like internal_error() but do not call abort(), to avoid tests using
904 * test_unknown() and test_void() causing Vim to exit.
905 */
906 void
907internal_error_no_abort(char *where)
908{
Bram Moolenaar097c5372023-05-24 21:02:24 +0100909 // Give a generic error which is translated. The error itself may not be
910 // translated, it almost never shows.
911 emsg_core(_(e_internal_error_please_report_a_bug));
912
913 semsg(_(e_internal_error_str), where);
Bram Moolenaardd589232020-02-29 17:38:12 +0100914}
Dominique Pelle748b3082022-01-08 12:41:16 +0000915#endif
Bram Moolenaardd589232020-02-29 17:38:12 +0100916
Bram Moolenaar85a20022019-12-21 18:25:54 +0100917// emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918
Bram Moolenaardf177f62005-02-22 08:39:57 +0000919 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100920emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000921{
cero19881d87e112023-02-16 15:03:12 +0000922 semsg(_(e_invalid_register_name_str), transchar_buf(NULL, name));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000923}
924
Dominique Pelle748b3082022-01-08 12:41:16 +0000925#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100927 * Give an error message which contains %s for "name[len]".
928 */
929 void
930emsg_namelen(char *msg, char_u *name, int len)
931{
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000932 char_u *copy = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100933
934 semsg(msg, copy == NULL ? "NULL" : (char *)copy);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100935 vim_free(copy);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100936}
Dominique Pelle748b3082022-01-08 12:41:16 +0000937#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100938
939/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 * Like msg(), but truncate to a single line if p_shm contains 't', or when
941 * "force" is TRUE. This truncates in another way as for normal messages.
942 * Careful: The string may be changed by msg_may_trunc()!
943 * Returns a pointer to the printed message, if wait_return() not called.
944 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100945 char *
946msg_trunc_attr(char *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947{
948 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100949 char *ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950
Bram Moolenaar85a20022019-12-21 18:25:54 +0100951 // Add message to history before truncating
Bram Moolenaar32526b32019-01-19 17:43:09 +0100952 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953
Bram Moolenaar32526b32019-01-19 17:43:09 +0100954 ts = (char *)msg_may_trunc(force, (char_u *)s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955
956 msg_hist_off = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100957 n = msg_attr(ts, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 msg_hist_off = FALSE;
959
960 if (n)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100961 return ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 return NULL;
963}
964
965/*
966 * Check if message "s" should be truncated at the start (for filenames).
967 * Return a pointer to where the truncated message starts.
968 * Note: May change the message by replacing a character with '<'.
969 */
970 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100971msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972{
973 int n;
974 int room;
975
Bram Moolenaar9198de32022-08-27 21:30:03 +0100976 // If 'cmdheight' is zero or something unexpected happened "room" may be
977 // negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
Bram Moolenaar0fbc9262022-06-26 11:17:10 +0100979 if (room > 0 && (force || (shortmess(SHM_TRUNC) && !exmode_active))
Bram Moolenaara2a89732022-08-31 14:46:18 +0100980 && (n = (int)STRLEN(s) - room) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 if (has_mbyte)
983 {
984 int size = vim_strsize(s);
985
Bram Moolenaar85a20022019-12-21 18:25:54 +0100986 // There may be room anyway when there are multibyte chars.
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000987 if (size <= room)
988 return s;
989
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 for (n = 0; size >= room; )
991 {
992 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000993 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 }
995 --n;
996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 s += n;
998 *s = '<';
999 }
1000 return s;
1001}
1002
1003 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001004add_msg_hist(
1005 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001006 int len, // -1 for undetermined length
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001007 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008{
1009 struct msg_hist *p;
1010
1011 if (msg_hist_off || msg_silent != 0)
1012 return;
1013
Bram Moolenaar85a20022019-12-21 18:25:54 +01001014 // allocate an entry and add the message at the end of the history
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001015 p = ALLOC_ONE(struct msg_hist);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001016 if (p == NULL)
1017 return;
1018
1019 if (len < 0)
1020 len = (int)STRLEN(s);
1021 // remove leading and trailing newlines
1022 while (len > 0 && *s == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001024 ++s;
1025 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 }
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001027 while (len > 0 && s[len - 1] == '\n')
1028 --len;
1029 p->msg = vim_strnsave(s, len);
1030 p->next = NULL;
1031 p->attr = attr;
1032 if (last_msg_hist != NULL)
1033 last_msg_hist->next = p;
1034 last_msg_hist = p;
1035 if (first_msg_hist == NULL)
1036 first_msg_hist = last_msg_hist;
1037 ++msg_hist_len;
Shougo Matsushita9f860a12024-11-24 13:48:05 +01001038
1039 check_msg_hist();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040}
1041
1042/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001043 * Delete the first (oldest) message from the history.
1044 * Returns FAIL if there are no messages.
1045 */
1046 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001047delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001048{
1049 struct msg_hist *p;
1050
1051 if (msg_hist_len <= 0)
1052 return FAIL;
1053 p = first_msg_hist;
1054 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +00001055 if (first_msg_hist == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001056 last_msg_hist = NULL; // history is empty
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001057 vim_free(p->msg);
1058 vim_free(p);
1059 --msg_hist_len;
1060 return OK;
1061}
1062
Shougo Matsushita9f860a12024-11-24 13:48:05 +01001063 void
1064check_msg_hist(void)
1065{
1066 // Don't let the message history get too big
1067 while (msg_hist_len > 0 && msg_hist_len > p_mhi)
1068 (void)delete_first_msg();
1069}
1070
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001071/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 * ":messages" command.
1073 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 void
Bram Moolenaar52196b22016-04-14 17:52:41 +02001075ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076{
1077 struct msg_hist *p;
1078 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001079 int c = 0;
1080
1081 if (STRCMP(eap->arg, "clear") == 0)
1082 {
1083 int keep = eap->addr_count == 0 ? 0 : eap->line2;
1084
1085 while (msg_hist_len > keep)
1086 (void)delete_first_msg();
1087 return;
1088 }
1089
1090 if (*eap->arg != NUL)
1091 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001092 emsg(_(e_invalid_argument));
Bram Moolenaar451f8492016-04-14 17:16:22 +02001093 return;
1094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095
1096 msg_hist_off = TRUE;
1097
Bram Moolenaar451f8492016-04-14 17:16:22 +02001098 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001099 if (eap->addr_count != 0)
1100 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001101 // Count total messages
Bram Moolenaar451f8492016-04-14 17:16:22 +02001102 for (; p != NULL && !got_int; p = p->next)
1103 c++;
1104
1105 c -= eap->line2;
1106
Bram Moolenaar85a20022019-12-21 18:25:54 +01001107 // Skip without number of messages specified
Bram Moolenaar451f8492016-04-14 17:16:22 +02001108 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
1109 p = p->next, c--);
1110 }
1111
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001112 if (p == first_msg_hist)
1113 {
Bram Moolenaar41f69182020-04-25 15:45:37 +02001114#ifdef FEAT_MULTI_LANG
1115 s = get_mess_lang();
1116#else
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001117 s = mch_getenv((char_u *)"LANG");
Bram Moolenaar41f69182020-04-25 15:45:37 +02001118#endif
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001119 if (s != NULL && *s != NUL)
Bram Moolenaar0c183192018-06-28 14:54:43 +02001120 // The next comment is extracted by xgettext and put in po file for
1121 // translators to read.
Bram Moolenaar32526b32019-01-19 17:43:09 +01001122 msg_attr(
Bram Moolenaar0c183192018-06-28 14:54:43 +02001123 // Translator: Please replace the name and email address
1124 // with the appropriate text for your translation.
Christian Brabandte978b452023-08-13 10:33:05 +02001125 _("Messages maintainer: The Vim Project"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01001126 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001127 }
1128
Bram Moolenaar85a20022019-12-21 18:25:54 +01001129 // Display what was not skipped.
Bram Moolenaar451f8492016-04-14 17:16:22 +02001130 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 if (p->msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001132 msg_attr((char *)p->msg, p->attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133
1134 msg_hist_off = FALSE;
1135}
1136
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001137#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138/*
1139 * Call this after prompting the user. This will avoid a hit-return message
1140 * and a delay.
1141 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001142 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001143msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144{
1145 need_wait_return = FALSE;
1146 emsg_on_display = FALSE;
1147 cmdline_row = msg_row;
1148 msg_col = 0;
1149 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001150 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151}
1152#endif
1153
1154/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01001155 * Wait for the user to hit a key (normally Enter).
1156 * If "redraw" is TRUE, clear and redraw the screen.
1157 * If "redraw" is FALSE, just redraw the screen.
1158 * If "redraw" is -1, don't redraw at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 */
1160 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001161wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162{
1163 int c;
1164 int oldState;
1165 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 int had_got_int;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001167 int save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001168 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169
1170 if (redraw == TRUE)
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01001171 set_must_redraw(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172
Bram Moolenaar85a20022019-12-21 18:25:54 +01001173 // If using ":silent cmd", don't wait for a return. Also don't set
1174 // need_wait_return to do it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 if (msg_silent != 0)
1176 return;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01001177#ifdef HAS_MESSAGE_WINDOW
1178 if (in_echowindow)
1179 return;
1180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001182 /*
1183 * When inside vgetc(), we can't wait for a typed character at all.
1184 * With the global command (and some others) we only need one return at
1185 * the end. Adjust cmdline_row to avoid the next message overwriting the
1186 * last one.
1187 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001188 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001190 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 if (no_wait_return)
1192 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 if (!exmode_active)
1194 cmdline_row = msg_row;
1195 return;
1196 }
1197
Bram Moolenaar85a20022019-12-21 18:25:54 +01001198 redir_off = TRUE; // don't redirect this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 oldState = State;
1200 if (quit_more)
1201 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001202 c = CAR; // just pretend CR was hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 quit_more = FALSE;
1204 got_int = FALSE;
1205 }
1206 else if (exmode_active)
1207 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001208 msg_puts(" "); // make sure the cursor is on the right line
1209 c = CAR; // no need for a return in ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 got_int = FALSE;
1211 }
1212 else
1213 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001214 // Make sure the hit-return prompt is on screen when 'guioptions' was
1215 // just changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 screenalloc(FALSE);
1217
Bram Moolenaar24959102022-05-07 20:01:16 +01001218 State = MODE_HITRETURN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001221 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001223 cmdline_row = msg_row;
1224
Bram Moolenaar85a20022019-12-21 18:25:54 +01001225 // Avoid the sequence that the user types ":" at the hit-return prompt
1226 // to start an Ex command, but the file-changed dialog gets in the
1227 // way.
Bram Moolenaarec38d692013-04-24 15:12:32 +02001228 if (need_check_timestamps)
1229 check_timestamps(FALSE);
1230
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 hit_return_msg();
1232
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 do
1234 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001235 // Remember "got_int", if it is set vgetc() probably returns a
1236 // CTRL-C, but we need to loop then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001238
Bram Moolenaar85a20022019-12-21 18:25:54 +01001239 // Don't do mappings here, we put the character back in the
1240 // typeahead buffer.
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001241 ++no_mapping;
1242 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001243
Bram Moolenaar85a20022019-12-21 18:25:54 +01001244 // Temporarily disable Recording. If Recording is active, the
1245 // character will be recorded later, since it will be added to the
1246 // typebuf after the loop
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001247 save_reg_recording = reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001248 save_scriptout = scriptout;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001249 reg_recording = 0;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001250 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001252 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001254 --no_mapping;
1255 --allow_keys;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001256 reg_recording = save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001257 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001258
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259#ifdef FEAT_CLIPBOARD
Bram Moolenaar85a20022019-12-21 18:25:54 +01001260 // Strange way to allow copying (yanking) a modeless selection at
1261 // the hit-enter prompt. Use CTRL-Y, because the same is used in
1262 // Cmdline-mode and it's harmless when there is no selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1264 {
1265 clip_copy_modeless_selection(TRUE);
1266 c = K_IGNORE;
1267 }
1268#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001269
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001270 /*
1271 * Allow scrolling back in the messages.
1272 * Also accept scroll-down commands when messages fill the screen,
1273 * to avoid that typing one 'j' too many makes the messages
1274 * disappear.
1275 */
1276 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001277 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001278 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1279 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001280 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001281 if (msg_scrolled > Rows)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001282 // scroll back to show older messages
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001283 do_more_prompt(c);
1284 else
1285 {
1286 msg_didout = FALSE;
1287 c = K_IGNORE;
1288 msg_col =
1289#ifdef FEAT_RIGHTLEFT
1290 cmdmsg_rl ? Columns - 1 :
1291#endif
1292 0;
1293 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001294 if (quit_more)
1295 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001296 c = CAR; // just pretend CR was hit
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001297 quit_more = FALSE;
1298 got_int = FALSE;
1299 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001300 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001301 {
1302 c = K_IGNORE;
1303 hit_return_msg();
1304 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001305 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001306 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001307 && (c == 'j' || c == 'd' || c == 'f'
1308 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001309 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 } while ((had_got_int && c == Ctrl_C)
1312 || c == K_IGNORE
1313#ifdef FEAT_GUI
1314 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1317 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1318 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001319 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001321 || c == K_MOUSEMOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 || (!mouse_has(MOUSE_RETURN)
1323 && mouse_row < msg_row
1324 && (c == K_LEFTMOUSE
1325 || c == K_MIDDLEMOUSE
1326 || c == K_RIGHTMOUSE
1327 || c == K_X1MOUSE
1328 || c == K_X2MOUSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 );
1330 ui_breakcheck();
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001331
1332 // Avoid that the mouse-up event causes Visual mode to start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1334 || c == K_X1MOUSE || c == K_X2MOUSE)
1335 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001336 else if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001338 // Put the character back in the typeahead buffer. Don't use the
1339 // stuff buffer, because lmaps wouldn't work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001340 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001341 do_redraw = TRUE; // need a redraw even though there is
1342 // typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 }
1345 redir_off = FALSE;
1346
1347 /*
1348 * If the user hits ':', '?' or '/' we get a command line from the next
1349 * line.
1350 */
1351 if (c == ':' || c == '?' || c == '/')
1352 {
1353 if (!exmode_active)
1354 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001355 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 do_redraw = FALSE;
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001357#ifdef FEAT_TERMINAL
1358 skip_term_loop = TRUE;
1359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 }
1361
1362 /*
1363 * If the window size changed set_shellsize() will redraw the screen.
1364 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1365 * typed.
1366 */
1367 tmpState = State;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001368 State = oldState; // restore State before set_shellsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 msg_check();
1371
1372#if defined(UNIX) || defined(VMS)
1373 /*
1374 * When switching screens, we need to output an extra newline on exit.
1375 */
1376 if (swapping_screen() && !termcap_active)
1377 newline_on_exit = TRUE;
1378#endif
1379
1380 need_wait_return = FALSE;
1381 did_wait_return = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001382 emsg_on_display = FALSE; // can delete error message now
1383 lines_left = -1; // reset lines_left at next msg_start()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 reset_last_sourcing();
1385 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1386 (Rows - cmdline_row - 1) * Columns + sc_col)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001387 VIM_CLEAR(keep_msg); // don't redisplay message, it's too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388
Bram Moolenaar24959102022-05-07 20:01:16 +01001389 if (tmpState == MODE_SETWSIZE) // got resize event while in vgetc()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001391 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 shell_resized();
1393 }
1394 else if (!skip_redraw
1395 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1396 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001397 starttermcap(); // start termcap before redrawing
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001398 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 }
1400}
1401
1402/*
1403 * Write the hit-return prompt.
1404 */
1405 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001406hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001408 int save_p_more = p_more;
1409
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001410 p_more = FALSE; // don't want to see this message when scrolling back
Bram Moolenaar85a20022019-12-21 18:25:54 +01001411 if (msg_didout) // start on a new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 msg_putchar('\n');
1413 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001414 msg_puts(_("Interrupt: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415
Bram Moolenaar32526b32019-01-19 17:43:09 +01001416 msg_puts_attr(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 if (!msg_use_printf())
1418 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001419 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420}
1421
1422/*
1423 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1424 */
1425 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001426set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427{
1428 vim_free(keep_msg);
1429 if (s != NULL && msg_silent == 0)
1430 keep_msg = vim_strsave(s);
1431 else
1432 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001433 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001434 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435}
1436
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001437/*
1438 * If there currently is a message being displayed, set "keep_msg" to it, so
1439 * that it will be displayed again after redraw.
1440 */
1441 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001442set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001443{
1444 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01001445 && (State & MODE_NORMAL))
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001446 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1447}
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001448
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449/*
1450 * Prepare for outputting characters in the command line.
1451 */
1452 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001453msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
1455 int did_return = FALSE;
1456
zeertzjq40ed6712023-11-23 20:37:01 +01001457 if (msg_row < cmdline_row)
1458 msg_row = cmdline_row;
1459
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001460 if (!msg_silent)
Rob Pilling726f7f92022-01-20 14:44:38 +00001461 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001462 VIM_CLEAR(keep_msg);
Rob Pilling726f7f92022-01-20 14:44:38 +00001463 need_fileinfo = FALSE;
1464 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00001465
1466#ifdef FEAT_EVAL
Bram Moolenaar3b474dc2022-09-01 17:01:32 +01001467 if (need_clr_eos)
Bram Moolenaara7241f52008-06-24 20:39:31 +00001468 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001469 // Halfway an ":echo" command and getting an (error) message: clear
1470 // any text from the command.
Bram Moolenaara7241f52008-06-24 20:39:31 +00001471 need_clr_eos = FALSE;
1472 msg_clr_eos();
1473 }
1474#endif
1475
Bram Moolenaard54af2e2022-08-27 22:05:13 +01001476#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +01001477 if (in_echowindow)
Bram Moolenaar9198de32022-08-27 21:30:03 +01001478 {
Bram Moolenaar37fef162022-08-29 18:16:32 +01001479 if (popup_message_win_visible()
1480 && ((msg_col > 0 && (msg_scroll || !full_screen))
1481 || in_echowindow))
Bram Moolenaar9198de32022-08-27 21:30:03 +01001482 {
1483 win_T *wp = popup_get_message_win();
1484
Bram Moolenaarf2fb54f2022-08-28 20:58:51 +01001485 // start a new line
Bram Moolenaar9198de32022-08-27 21:30:03 +01001486 curbuf = wp->w_buffer;
1487 ml_append(wp->w_buffer->b_ml.ml_line_count,
1488 (char_u *)"", (colnr_T)0, FALSE);
1489 curbuf = curwin->w_buffer;
1490 }
1491 msg_col = 0;
1492 }
Bram Moolenaard54af2e2022-08-27 22:05:13 +01001493 else
1494#endif
1495 if (!msg_scroll && full_screen) // overwrite last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 {
1497 msg_row = cmdline_row;
1498 msg_col =
1499#ifdef FEAT_RIGHTLEFT
1500 cmdmsg_rl ? Columns - 1 :
1501#endif
1502 0;
1503 }
Bram Moolenaara2a89732022-08-31 14:46:18 +01001504 else if (msg_didout || in_echowindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 {
Bram Moolenaar309c4e02022-08-29 12:23:39 +01001506 // start message on next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 msg_putchar('\n');
1508 did_return = TRUE;
1509 if (exmode_active != EXMODE_NORMAL)
1510 cmdline_row = msg_row;
1511 }
1512 if (!msg_didany || lines_left < 0)
1513 msg_starthere();
1514 if (msg_silent == 0)
1515 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001516 msg_didout = FALSE; // no output on current line yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 cursor_off();
1518 }
1519
Bram Moolenaar85a20022019-12-21 18:25:54 +01001520 // when redirecting, may need to start a new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 if (!did_return)
1522 redir_write((char_u *)"\n", -1);
1523}
1524
1525/*
1526 * Note that the current msg position is where messages start.
1527 */
1528 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001529msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530{
1531 lines_left = cmdline_row;
1532 msg_didany = FALSE;
1533}
1534
1535 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001536msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537{
1538 msg_putchar_attr(c, 0);
1539}
1540
1541 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001542msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543{
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001544 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545
1546 if (IS_SPECIAL(c))
1547 {
1548 buf[0] = K_SPECIAL;
1549 buf[1] = K_SECOND(c);
1550 buf[2] = K_THIRD(c);
1551 buf[3] = NUL;
1552 }
1553 else
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001554 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001555 msg_puts_attr((char *)buf, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556}
1557
1558 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001559msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001561 char buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562
Bram Moolenaar32526b32019-01-19 17:43:09 +01001563 sprintf(buf, "%ld", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 msg_puts(buf);
1565}
1566
1567 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001568msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569{
1570 msg_home_replace_attr(fname, 0);
1571}
1572
1573#if defined(FEAT_FIND_ID) || defined(PROTO)
1574 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001575msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001577 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578}
1579#endif
1580
1581 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001582msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583{
1584 char_u *name;
1585
1586 name = home_replace_save(NULL, fname);
1587 if (name != NULL)
1588 msg_outtrans_attr(name, attr);
1589 vim_free(name);
1590}
1591
1592/*
1593 * Output 'len' characters in 'str' (including NULs) with translation
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001594 * if 'len' is -1, output up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 * Use attributes 'attr'.
1596 * Return the number of characters it takes on the screen.
1597 */
1598 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001599msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600{
1601 return msg_outtrans_attr(str, 0);
1602}
1603
1604 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001605msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606{
1607 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1608}
1609
1610 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001611msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612{
1613 return msg_outtrans_len_attr(str, len, 0);
1614}
1615
1616/*
1617 * Output one character at "p". Return pointer to the next character.
1618 * Handles multi-byte characters.
1619 */
1620 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001621msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 int l;
1624
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001625 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 {
1627 msg_outtrans_len_attr(p, l, attr);
1628 return p + l;
1629 }
cero19881d87e112023-02-16 15:03:12 +00001630 msg_puts_attr((char *)transchar_byte_buf(NULL, *p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 return p + 1;
1632}
1633
1634 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001635msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636{
1637 int retval = 0;
1638 char_u *str = msgstr;
1639 char_u *plain_start = msgstr;
1640 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 int mb_l;
1642 int c;
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001643 int save_got_int = got_int;
1644
1645 // Only quit when got_int was set in here.
1646 got_int = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02001648 if (attr == 0)
1649 attr = HL_ATTR(HLF_MSG);
1650
Bram Moolenaar85a20022019-12-21 18:25:54 +01001651 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 if (attr & MSG_HIST)
1653 {
1654 add_msg_hist(str, len, attr);
1655 attr &= ~MSG_HIST;
1656 }
1657
Bram Moolenaar800cdbb2023-06-15 16:40:02 +01001658 // When drawing over the command line no need to clear it later or remove
1659 // the mode message.
Bram Moolenaarda51ad52023-06-15 18:44:50 +01001660 if (msg_row >= cmdline_row && msg_col == 0)
Bram Moolenaar800cdbb2023-06-15 16:40:02 +01001661 {
1662 clear_cmdline = FALSE;
1663 mode_displayed = FALSE;
1664 }
1665
Bram Moolenaar85a20022019-12-21 18:25:54 +01001666 // If the string starts with a composing character first draw a space on
1667 // which the composing char can be drawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
Bram Moolenaar32526b32019-01-19 17:43:09 +01001669 msg_puts_attr(" ", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670
1671 /*
1672 * Go over the string. Special characters are translated and printed.
1673 * Normal characters are printed several at a time.
1674 */
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001675 while (--len >= 0 && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001678 // Don't include composing chars after the end.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001679 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001681 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 else
1683 mb_l = 1;
1684 if (has_mbyte && mb_l > 1)
1685 {
1686 c = (*mb_ptr2char)(str);
1687 if (vim_isprintc(c))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001688 // printable multi-byte char: count the cells.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 retval += (*mb_ptr2cells)(str);
1690 else
1691 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001692 // unprintable multi-byte char: print the printable chars so
1693 // far and the translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001695 msg_puts_attr_len((char *)plain_start,
1696 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 plain_start = str + mb_l;
cero19881d87e112023-02-16 15:03:12 +00001698 msg_puts_attr((char *)transchar_buf(NULL, c),
Bram Moolenaar32526b32019-01-19 17:43:09 +01001699 attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 retval += char2cells(c);
1701 }
1702 len -= mb_l - 1;
1703 str += mb_l;
1704 }
1705 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 {
cero19881d87e112023-02-16 15:03:12 +00001707 s = transchar_byte_buf(NULL, *str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 if (s[1] != NUL)
1709 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001710 // unprintable char: print the printable chars so far and the
1711 // translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001713 msg_puts_attr_len((char *)plain_start,
1714 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 plain_start = str + 1;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001716 msg_puts_attr((char *)s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001717 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001719 else
1720 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 ++str;
1722 }
1723 }
1724
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001725 if (str > plain_start && !got_int)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001726 // print the printable chars at the end
Bram Moolenaar32526b32019-01-19 17:43:09 +01001727 msg_puts_attr_len((char *)plain_start, (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001729 got_int |= save_got_int;
1730
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 return retval;
1732}
1733
1734#if defined(FEAT_QUICKFIX) || defined(PROTO)
1735 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001736msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737{
1738 int i;
1739 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1740
1741 arg = skipwhite(arg);
1742 for (i = 5; *arg && i >= 0; --i)
1743 if (*arg++ != str[i])
1744 break;
1745 if (i < 0)
1746 {
1747 msg_putchar('\n');
1748 for (i = 0; rs[i]; ++i)
1749 msg_putchar(rs[i] - 3);
1750 }
1751}
1752#endif
1753
1754/*
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001755 * Output the string 'str' up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 * Return the number of characters it takes on the screen.
1757 *
1758 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1759 * following character and shown as <F1>, <S-Up> etc. Any other character
1760 * which is not printable shown in <> form.
1761 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1762 * If a character is displayed in one of these special ways, is also
1763 * highlighted (its highlight name is '8' in the p_hl variable).
1764 * Otherwise characters are not highlighted.
1765 * This function is used to show mappings, where we want to see how to type
1766 * the character/string -- webb
1767 */
1768 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001769msg_outtrans_special(
1770 char_u *strstart,
Bram Moolenaar725310d2019-04-24 23:08:23 +02001771 int from, // TRUE for lhs of a mapping
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001772 int maxlen) // screen columns, 0 for unlimited
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773{
1774 char_u *str = strstart;
1775 int retval = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001776 char *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 int attr;
1778 int len;
1779
Bram Moolenaar8820b482017-03-16 17:23:31 +01001780 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 while (*str != NUL)
1782 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001783 // Leading and trailing spaces need to be displayed in <> form.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 if ((str == strstart || str[1] == NUL) && *str == ' ')
1785 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001786 text = "<Space>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 ++str;
1788 }
1789 else
zeertzjqcdc83932022-09-12 13:38:41 +01001790 text = (char *)str2special(&str, from, FALSE);
zeertzjq0519ce02022-05-09 12:16:19 +01001791 if (text[0] != NUL && text[1] == NUL)
1792 // single-byte character or illegal byte
cero19881d87e112023-02-16 15:03:12 +00001793 text = (char *)transchar_byte_buf(NULL, (char_u)text[0]);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001794 len = vim_strsize((char_u *)text);
Bram Moolenaar725310d2019-04-24 23:08:23 +02001795 if (maxlen > 0 && retval + len >= maxlen)
1796 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001797 // Highlight special keys
Bram Moolenaar32526b32019-01-19 17:43:09 +01001798 msg_puts_attr(text, len > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001799 && (*mb_ptr2len)((char_u *)text) <= 1 ? attr : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 retval += len;
1801 }
1802 return retval;
1803}
1804
Martin Tournoijba43e762022-10-13 22:12:15 +01001805#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarbd743252010-10-20 21:23:33 +02001806/*
1807 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1808 * strings, in an allocated string.
1809 */
1810 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001811str2special_save(
1812 char_u *str,
zeertzjqcdc83932022-09-12 13:38:41 +01001813 int replace_spaces, // TRUE to replace " " with "<Space>".
1814 // used for the lhs of mapping and keytrans().
1815 int replace_lt) // TRUE to replace "<" with "<lt>".
Bram Moolenaarbd743252010-10-20 21:23:33 +02001816{
1817 garray_T ga;
1818 char_u *p = str;
1819
1820 ga_init2(&ga, 1, 40);
1821 while (*p != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01001822 ga_concat(&ga, str2special(&p, replace_spaces, replace_lt));
Bram Moolenaarbd743252010-10-20 21:23:33 +02001823 ga_append(&ga, NUL);
1824 return (char_u *)ga.ga_data;
1825}
1826#endif
1827
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828/*
1829 * Return the printable string for the key codes at "*sp".
zeertzjq0519ce02022-05-09 12:16:19 +01001830 * On illegal byte return a string with only that byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 * Used for translating the lhs or rhs of a mapping to printable chars.
1832 * Advances "sp" to the next code.
1833 */
1834 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001835str2special(
1836 char_u **sp,
zeertzjqcdc83932022-09-12 13:38:41 +01001837 int replace_spaces, // TRUE to replace " " with "<Space>".
1838 // used for the lhs of mapping and keytrans().
1839 int replace_lt) // TRUE to replace "<" with "<lt>".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840{
1841 int c;
1842 static char_u buf[7];
1843 char_u *str = *sp;
1844 int modifiers = 0;
1845 int special = FALSE;
1846
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 if (has_mbyte)
1848 {
1849 char_u *p;
1850
Bram Moolenaar85a20022019-12-21 18:25:54 +01001851 // Try to un-escape a multi-byte character. Return the un-escaped
1852 // string if it is a multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 p = mb_unescape(sp);
1854 if (p != NULL)
1855 return p;
1856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857
1858 c = *str;
zeertzjq1bed9932023-08-30 19:38:24 +02001859 if ((c == K_SPECIAL
1860#ifdef FEAT_GUI
1861 || c == CSI
1862#endif
1863 ) && str[1] != NUL && str[2] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 {
1865 if (str[1] == KS_MODIFIER)
1866 {
1867 modifiers = str[2];
1868 str += 3;
1869 c = *str;
1870 }
zeertzjq1bed9932023-08-30 19:38:24 +02001871 if ((c == K_SPECIAL
1872#ifdef FEAT_GUI
1873 || c == CSI
1874#endif
1875 ) && str[1] != NUL && str[2] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 {
1877 c = TO_SPECIAL(str[1], str[2]);
1878 str += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001880 if (IS_SPECIAL(c) || modifiers) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 special = TRUE;
1882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883
zeertzjq0519ce02022-05-09 12:16:19 +01001884 if (has_mbyte && !IS_SPECIAL(c) && MB_BYTE2LEN(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 {
zeertzjqac402f42022-05-04 18:51:43 +01001886 char_u *p;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001887
zeertzjqac402f42022-05-04 18:51:43 +01001888 *sp = str;
1889 // Try to un-escape a multi-byte character after modifiers.
1890 p = mb_unescape(sp);
zeertzjq0519ce02022-05-09 12:16:19 +01001891 if (p != NULL)
1892 // Since 'special' is TRUE the multi-byte character 'c' will be
1893 // processed by get_special_key_name()
1894 c = (*mb_ptr2char)(p);
1895 else
1896 // illegal byte
1897 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001899 else
Bram Moolenaar083692d2022-06-29 21:16:58 +01001900 // single-byte character, NUL or illegal byte
1901 *sp = str + (*str == NUL ? 0 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902
zeertzjq0519ce02022-05-09 12:16:19 +01001903 // Make special keys and C0 control characters in <> form, also <M-Space>.
zeertzjqcdc83932022-09-12 13:38:41 +01001904 if (special
1905 || c < ' '
1906 || (replace_spaces && c == ' ')
1907 || (replace_lt && c == '<'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 return get_special_key_name(c, modifiers);
1909 buf[0] = c;
1910 buf[1] = NUL;
1911 return buf;
1912}
1913
1914/*
1915 * Translate a key sequence into special key names.
1916 */
1917 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001918str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919{
1920 char_u *s;
1921
1922 *buf = NUL;
1923 while (*sp)
1924 {
zeertzjqcdc83932022-09-12 13:38:41 +01001925 s = str2special(&sp, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1927 STRCAT(buf, s);
1928 }
1929}
1930
1931/*
1932 * print line for :print or :list command
1933 */
1934 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001935msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936{
1937 int c;
1938 int col = 0;
1939 int n_extra = 0;
1940 int c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001941 int c_final = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001942 char_u *p_extra = NULL; // init to make SASC shut up
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001944 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 char_u *trail = NULL;
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001946 char_u *lead = NULL;
zeertzjqf14b8ba2021-09-10 16:58:30 +02001947 int in_multispace = FALSE;
1948 int multispace_pos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 int l;
1950 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951
Bram Moolenaardf177f62005-02-22 08:39:57 +00001952 if (curwin->w_p_list)
1953 list = TRUE;
1954
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001955 if (list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 {
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001957 // find start of trailing whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +01001958 if (curwin->w_lcs_chars.trail)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001959 {
1960 trail = s + STRLEN(s);
1961 while (trail > s && VIM_ISWHITE(trail[-1]))
1962 --trail;
1963 }
1964 // find end of leading whitespace
Bram Moolenaarb8329db2022-07-06 13:31:28 +01001965 if (curwin->w_lcs_chars.lead
1966 || curwin->w_lcs_chars.leadmultispace != NULL)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001967 {
1968 lead = s;
1969 while (VIM_ISWHITE(lead[0]))
1970 lead++;
1971 // in a line full of spaces all of them are treated as trailing
1972 if (*lead == NUL)
1973 lead = NULL;
1974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 }
1976
Bram Moolenaar85a20022019-12-21 18:25:54 +01001977 // output a space for an empty line, otherwise the line will be
1978 // overwritten
Bram Moolenaareed9d462021-02-15 20:38:25 +01001979 if (*s == NUL && !(list && curwin->w_lcs_chars.eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 msg_putchar(' ');
1981
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001982 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001984 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 {
1986 --n_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001987 if (n_extra == 0 && c_final)
1988 c = c_final;
1989 else if (c_extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 c = c_extra;
1991 else
1992 c = *p_extra++;
1993 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001994 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 {
1996 col += (*mb_ptr2cells)(s);
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001997 if (l >= MB_MAXBYTES)
1998 {
Bram Moolenaar29d2f452020-12-04 19:42:52 +01001999 STRCPY(buf, "?");
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01002000 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002001 else if (curwin->w_lcs_chars.nbsp != NUL && list
Bram Moolenaar73284b92015-05-04 17:28:22 +02002002 && (mb_ptr2char(s) == 160
2003 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01002004 {
Bram Moolenaar74ac29c2022-06-15 12:12:44 +01002005 int len = mb_char2bytes(curwin->w_lcs_chars.nbsp, buf);
2006
2007 buf[len] = NUL;
Bram Moolenaaracf17282011-02-01 17:12:25 +01002008 }
2009 else
2010 {
2011 mch_memmove(buf, s, (size_t)l);
2012 buf[l] = NUL;
2013 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01002014 msg_puts((char *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 s += l;
2016 continue;
2017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 else
2019 {
2020 attr = 0;
2021 c = *s++;
zeertzjqabc80812023-09-24 23:32:18 +02002022 if (list)
2023 {
2024 in_multispace = c == ' ' && (*s == ' '
2025 || (col > 0 && s[-2] == ' '));
2026 if (!in_multispace)
2027 multispace_pos = 0;
2028 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002029 if (c == TAB && (!list || curwin->w_lcs_chars.tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002031 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002032#ifdef FEAT_VARTABS
2033 n_extra = tabstop_padding(col, curbuf->b_p_ts,
2034 curbuf->b_p_vts_array) - 1;
2035#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002037#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00002038 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 {
2040 c = ' ';
2041 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01002042 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 }
2044 else
2045 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002046 c = (n_extra == 0 && curwin->w_lcs_chars.tab3)
2047 ? curwin->w_lcs_chars.tab3
2048 : curwin->w_lcs_chars.tab1;
2049 c_extra = curwin->w_lcs_chars.tab2;
2050 c_final = curwin->w_lcs_chars.tab3;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002051 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 }
2053 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002054 else if (c == 160 && list && curwin->w_lcs_chars.nbsp != NUL)
Bram Moolenaaracf17282011-02-01 17:12:25 +01002055 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002056 c = curwin->w_lcs_chars.nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002057 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01002058 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002059 else if (c == NUL && list && curwin->w_lcs_chars.eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 {
2061 p_extra = (char_u *)"";
2062 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01002063 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 n_extra = 1;
Bram Moolenaareed9d462021-02-15 20:38:25 +01002065 c = curwin->w_lcs_chars.eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002066 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 --s;
2068 }
2069 else if (c != NUL && (n = byte2cells(c)) > 1)
2070 {
2071 n_extra = n - 1;
cero19881d87e112023-02-16 15:03:12 +00002072 p_extra = transchar_byte_buf(NULL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01002074 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 c = *p_extra++;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002076 // Use special coloring to be able to distinguish <hex> from
2077 // the same in plain text.
Bram Moolenaar8820b482017-03-16 17:23:31 +01002078 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02002080 else if (c == ' ')
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002081 {
zeertzjqabc80812023-09-24 23:32:18 +02002082 if (lead != NULL && s <= lead && in_multispace
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002083 && curwin->w_lcs_chars.leadmultispace != NULL)
2084 {
2085 c = curwin->w_lcs_chars.leadmultispace[multispace_pos++];
zeertzjqb5f08012022-06-09 13:55:28 +01002086 if (curwin->w_lcs_chars.leadmultispace[multispace_pos]
2087 == NUL)
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002088 multispace_pos = 0;
2089 attr = HL_ATTR(HLF_8);
2090 }
zeertzjqb5f08012022-06-09 13:55:28 +01002091 else if (lead != NULL && s <= lead
2092 && curwin->w_lcs_chars.lead != NUL)
zeertzjqf14b8ba2021-09-10 16:58:30 +02002093 {
2094 c = curwin->w_lcs_chars.lead;
2095 attr = HL_ATTR(HLF_8);
2096 }
2097 else if (trail != NULL && s > trail)
2098 {
2099 c = curwin->w_lcs_chars.trail;
2100 attr = HL_ATTR(HLF_8);
2101 }
zeertzjqabc80812023-09-24 23:32:18 +02002102 else if (in_multispace
zeertzjqf14b8ba2021-09-10 16:58:30 +02002103 && curwin->w_lcs_chars.multispace != NULL)
2104 {
2105 c = curwin->w_lcs_chars.multispace[multispace_pos++];
2106 if (curwin->w_lcs_chars.multispace[multispace_pos] == NUL)
2107 multispace_pos = 0;
2108 attr = HL_ATTR(HLF_8);
2109 }
2110 else if (list && curwin->w_lcs_chars.space != NUL)
2111 {
2112 c = curwin->w_lcs_chars.space;
2113 attr = HL_ATTR(HLF_8);
2114 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02002115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 }
2117
2118 if (c == NUL)
2119 break;
2120
2121 msg_putchar_attr(c, attr);
2122 col++;
2123 }
2124 msg_clr_eos();
2125}
2126
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127/*
2128 * Use screen_puts() to output one multi-byte character.
2129 * Return the pointer "s" advanced to the next character.
2130 */
2131 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002132screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133{
2134 int cw;
2135
Bram Moolenaar85a20022019-12-21 18:25:54 +01002136 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 cw = (*mb_ptr2cells)(s);
2138 if (cw > 1 && (
2139#ifdef FEAT_RIGHTLEFT
2140 cmdmsg_rl ? msg_col <= 1 :
2141#endif
2142 msg_col == Columns - 1))
2143 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002144 // Doesn't fit, print a highlighted '>' to fill it up.
Bram Moolenaar8820b482017-03-16 17:23:31 +01002145 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 return s;
2147 }
2148
2149 screen_puts_len(s, l, msg_row, msg_col, attr);
2150#ifdef FEAT_RIGHTLEFT
2151 if (cmdmsg_rl)
2152 {
2153 msg_col -= cw;
2154 if (msg_col == 0)
2155 {
2156 msg_col = Columns;
2157 ++msg_row;
2158 }
2159 }
2160 else
2161#endif
2162 {
2163 msg_col += cw;
2164 if (msg_col >= Columns)
2165 {
2166 msg_col = 0;
2167 ++msg_row;
2168 }
2169 }
2170 return s + l;
2171}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172
2173/*
2174 * Output a string to the screen at position msg_row, msg_col.
2175 * Update msg_row and msg_col for the next message.
2176 */
2177 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002178msg_puts(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02002180 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181}
2182
2183 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002184msg_puts_title(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185{
Bram Moolenaar8820b482017-03-16 17:23:31 +01002186 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187}
2188
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189/*
2190 * Show a message in such a way that it always fits in the line. Cut out a
2191 * part in the middle and replace it with "..." when necessary.
2192 * Does not handle multi-byte characters!
2193 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002194 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002195msg_outtrans_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196{
2197 int slen = len;
2198 int room;
2199
2200 room = Columns - msg_col;
2201 if (len > room && room >= 20)
2202 {
2203 slen = (room - 3) / 2;
2204 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002205 msg_puts_attr("...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 }
2207 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
2208}
2209
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002210 void
2211msg_outtrans_long_attr(char_u *longstr, int attr)
2212{
2213 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr);
2214}
2215
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216/*
2217 * Basic function for writing a message with highlight attributes.
2218 */
2219 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002220msg_puts_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221{
2222 msg_puts_attr_len(s, -1, attr);
2223}
2224
2225/*
2226 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2227 * When "maxlen" is -1 there is no maximum length.
2228 * When "maxlen" is >= 0 the message is not put in the history.
2229 */
2230 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002231msg_puts_attr_len(char *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 /*
2234 * If redirection is on, also write to the redirection file.
2235 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01002236 redir_write((char_u *)str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237
2238 /*
2239 * Don't print anything when using ":silent cmd".
2240 */
2241 if (msg_silent != 0)
2242 return;
2243
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02002244 if (attr == 0)
2245 attr = HL_ATTR(HLF_MSG);
2246
Bram Moolenaar85a20022019-12-21 18:25:54 +01002247 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 if ((attr & MSG_HIST) && maxlen < 0)
2249 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002250 add_msg_hist((char_u *)str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 attr &= ~MSG_HIST;
2252 }
2253
Bram Moolenaare8070982019-10-12 17:07:06 +02002254 // When writing something to the screen after it has scrolled, requires a
2255 // wait-return prompt later. Needed when scrolling, resetting
2256 // need_wait_return after some prompt, and then outputting something
2257 // without scrolling
2258 // Not needed when only using CR to move the cursor.
2259 if (msg_scrolled != 0 && !msg_scrolled_ign && STRCMP(str, "\r") != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 need_wait_return = TRUE;
Bram Moolenaare8070982019-10-12 17:07:06 +02002261 msg_didany = TRUE; // remember that something was outputted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262
2263 /*
2264 * If there is no valid screen, use fprintf so we can see error messages.
2265 * If termcap is not active, we may be writing in an alternate console
2266 * window, cursor positioning may not work correctly (window size may be
2267 * different, e.g. for Win32 console) or we just don't know where the
2268 * cursor is.
2269 */
2270 if (msg_use_printf())
Bram Moolenaar32526b32019-01-19 17:43:09 +01002271 msg_puts_printf((char_u *)str, maxlen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002272 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002273 msg_puts_display((char_u *)str, maxlen, attr, FALSE);
Rob Pilling726f7f92022-01-20 14:44:38 +00002274
2275 need_fileinfo = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002276}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277
Bram Moolenaar9198de32022-08-27 21:30:03 +01002278// values for "where"
2279#define PUT_APPEND 0 // append to "lnum"
2280#define PUT_TRUNC 1 // replace "lnum"
2281#define PUT_BELOW 2 // add below "lnum"
2282 //
2283#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar9198de32022-08-27 21:30:03 +01002284/*
Bram Moolenaar24735f22022-08-30 15:44:22 +01002285 * Put text "t_s" until "end" in the message window.
Bram Moolenaar9198de32022-08-27 21:30:03 +01002286 * "where" specifies where to put the text.
2287 */
2288 static void
2289put_msg_win(win_T *wp, int where, char_u *t_s, char_u *end, linenr_T lnum)
2290{
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002291 char_u *p;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002292
Bram Moolenaar9198de32022-08-27 21:30:03 +01002293 if (where == PUT_BELOW)
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002294 {
2295 if (*end != NUL)
2296 {
2297 p = vim_strnsave(t_s, end - t_s);
2298 if (p == NULL)
2299 return;
2300 }
2301 else
2302 p = t_s;
2303 ml_append_buf(wp->w_buffer, lnum, p, (colnr_T)0, FALSE);
2304 if (p != t_s)
2305 vim_free(p);
2306 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01002307 else
2308 {
2309 char_u *newp;
2310
2311 curbuf = wp->w_buffer;
2312 if (where == PUT_APPEND)
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002313 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002314 newp = concat_str(ml_get(lnum), t_s);
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002315 if (newp == NULL)
2316 return;
2317 if (*end != NUL)
2318 newp[STRLEN(ml_get(lnum)) + (end - t_s)] = NUL;
2319 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01002320 else
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002321 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002322 newp = vim_strnsave(t_s, end - t_s);
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002323 if (newp == NULL)
2324 return;
2325 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01002326 ml_replace(lnum, newp, FALSE);
2327 curbuf = curwin->w_buffer;
2328 }
2329 redraw_win_later(wp, UPD_NOT_VALID);
2330
2331 // set msg_col so that a newline is written if needed
Bram Moolenaar24735f22022-08-30 15:44:22 +01002332 msg_col += (int)(end - t_s);
Bram Moolenaar9198de32022-08-27 21:30:03 +01002333}
2334#endif
2335
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002336/*
2337 * The display part of msg_puts_attr_len().
2338 * May be called recursively to display scroll-back text.
2339 */
2340 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002341msg_puts_display(
2342 char_u *str,
2343 int maxlen,
2344 int attr,
2345 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002346{
2347 char_u *s = str;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002348 char_u *t_s = str; // string from "t_s" to "s" is still todo
2349 int t_col = 0; // screen cells todo, 0 when "t_s" not used
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002350 int l;
2351 int cw;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002352 char_u *sb_str = str;
2353 int sb_col = msg_col;
2354 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002355 int did_last_char;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002356#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaard54af2e2022-08-27 22:05:13 +01002357 int where = PUT_APPEND;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002358 win_T *msg_win = NULL;
2359 linenr_T lnum = 1;
2360
Bram Moolenaara2a89732022-08-31 14:46:18 +01002361 if (in_echowindow)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002362 {
2363 msg_win = popup_get_message_win();
2364
2365 if (msg_win != NULL)
2366 {
2367 if (!popup_message_win_visible())
2368 {
2369 if (*str == NL)
2370 {
2371 // When not showing the message window and the output
2372 // starts with a NL show the message normally.
2373 msg_win = NULL;
2374 }
2375 else
2376 {
2377 // currently hidden, make it empty
2378 curbuf = msg_win->w_buffer;
2379 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2380 ml_delete(1);
2381 curbuf = curwin->w_buffer;
2382 }
2383 }
2384 else
2385 {
2386 lnum = msg_win->w_buffer->b_ml.ml_line_count;
2387 if (msg_col == 0)
2388 where = PUT_TRUNC;
2389 }
2390 }
2391 }
2392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393
2394 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002395 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 {
2397 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002398 * We are at the end of the screen line when:
2399 * - When outputting a newline.
2400 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002402 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403#ifdef FEAT_RIGHTLEFT
2404 cmdmsg_rl
2405 ? (
2406 msg_col <= 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002407 || (*s == TAB && msg_col <= 7)
2408 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 :
2410#endif
Bram Moolenaar0efd1bd2019-12-11 19:00:04 +01002411 ((*s != '\r' && msg_col + t_col >= Columns - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002414 && msg_col + t_col >= Columns - 2)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002416 /*
2417 * The screen is scrolled up when at the last row (some terminals
2418 * scroll automatically, some don't. To avoid problems we scroll
2419 * ourselves).
2420 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 if (t_col > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002422 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002423 // output postponed text
Bram Moolenaar9198de32022-08-27 21:30:03 +01002424#ifdef HAS_MESSAGE_WINDOW
2425 if (msg_win != NULL)
2426 {
2427 put_msg_win(msg_win, where, t_s, s, lnum);
2428 t_col = 0;
2429 where = PUT_BELOW;
2430 }
2431 else
2432#endif
2433 t_puts(&t_col, t_s, s, attr);
2434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435
Bram Moolenaar85a20022019-12-21 18:25:54 +01002436 // When no more prompt and no more room, truncate here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 if (msg_no_more && lines_left == 0)
2438 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439
Bram Moolenaar9198de32022-08-27 21:30:03 +01002440#ifdef HAS_MESSAGE_WINDOW
2441 if (msg_win == NULL)
2442#endif
2443 // Scroll the screen up one line.
2444 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445
2446 msg_row = Rows - 2;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002447 if (msg_col >= Columns) // can happen after screen resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 msg_col = Columns - 1;
2449
Bram Moolenaar85a20022019-12-21 18:25:54 +01002450 // Display char in last column before showing more-prompt.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002451 if (*s >= ' '
2452#ifdef FEAT_RIGHTLEFT
2453 && !cmdmsg_rl
2454#endif
2455 )
2456 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002457 if (has_mbyte)
2458 {
2459 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002460 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002461 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002462 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002463 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002464 s = screen_puts_mbyte(s, l, attr);
2465 }
2466 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002467 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002468 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002469 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002470 else
2471 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002472
2473 if (p_more)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002474 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002475 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2476
Bram Moolenaar9198de32022-08-27 21:30:03 +01002477#ifdef HAS_MESSAGE_WINDOW
2478 if (msg_win == NULL)
2479 {
2480#endif
2481 inc_msg_scrolled();
Bram Moolenaar13608d82022-08-29 15:06:50 +01002482 need_wait_return = TRUE; // may need wait_return() in main()
Bram Moolenaar9198de32022-08-27 21:30:03 +01002483 redraw_cmdline = TRUE;
2484 if (cmdline_row > 0 && !exmode_active)
2485 --cmdline_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486
Bram Moolenaar9198de32022-08-27 21:30:03 +01002487 /*
2488 * If screen is completely filled and 'more' is set then wait
2489 * for a character.
2490 */
2491 if (lines_left > 0)
2492 --lines_left;
2493#ifdef HAS_MESSAGE_WINDOW
2494 }
2495#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01002496 if (p_more && lines_left == 0 && State != MODE_HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 && !msg_no_more && !exmode_active)
2498 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002500 if (do_more_prompt(NUL))
2501 s = confirm_msg_tail;
2502#else
2503 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504#endif
2505 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002506 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002508
Bram Moolenaar85a20022019-12-21 18:25:54 +01002509 // When we displayed a char in last column need to check if there
2510 // is still more.
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002511 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002512 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 }
2514
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002515 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 || msg_col + t_col >= Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002518 && msg_col + t_col >= Columns - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002519 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2520 || *s == '\t' || *s == BELL))
Bram Moolenaar9198de32022-08-27 21:30:03 +01002521 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002522 // output any postponed text
Bram Moolenaar9198de32022-08-27 21:30:03 +01002523#ifdef HAS_MESSAGE_WINDOW
2524 if (msg_win != NULL)
2525 {
2526 put_msg_win(msg_win, where, t_s, s, lnum);
2527 t_col = 0;
2528 where = PUT_BELOW;
2529 }
2530 else
2531#endif
2532 t_puts(&t_col, t_s, s, attr);
2533 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002534
2535 if (wrap && p_more && !recurse)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002536 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002537 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538
Bram Moolenaar85a20022019-12-21 18:25:54 +01002539 if (*s == '\n') // go to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002541#ifdef HAS_MESSAGE_WINDOW
2542 if (msg_win != NULL)
2543 {
2544 // Ignore a NL when the buffer is empty, it is used to scroll
2545 // up the text.
2546 if ((msg_win->w_buffer->b_ml.ml_flags & ML_EMPTY) == 0)
2547 {
2548 put_msg_win(msg_win, PUT_BELOW, t_s, t_s, lnum);
2549 ++lnum;
2550 }
2551 }
2552 else
2553#endif
2554 msg_didout = FALSE; // remember that line is empty
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002555#ifdef FEAT_RIGHTLEFT
2556 if (cmdmsg_rl)
2557 msg_col = Columns - 1;
2558 else
2559#endif
2560 msg_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002561 if (++msg_row >= Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 msg_row = Rows - 1;
2563 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002564 else if (*s == '\r') // go to column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 {
2566 msg_col = 0;
Bram Moolenaard54af2e2022-08-27 22:05:13 +01002567#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar9198de32022-08-27 21:30:03 +01002568 where = PUT_TRUNC;
Bram Moolenaard54af2e2022-08-27 22:05:13 +01002569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002571 else if (*s == '\b') // go to previous char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 {
2573 if (msg_col)
2574 --msg_col;
2575 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002576 else if (*s == TAB) // translate Tab into spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002578#ifdef HAS_MESSAGE_WINDOW
2579 if (msg_win != NULL)
2580 msg_col = (msg_col + 7) % 8;
2581 else
2582#endif
2583 do
2584 msg_screen_putchar(' ', attr);
2585 while (msg_col & 7);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002587 else if (*s == BELL) // beep (from ":sh")
Bram Moolenaar165bc692015-07-21 17:53:25 +02002588 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 else
2590 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 if (has_mbyte)
2592 {
2593 cw = (*mb_ptr2cells)(s);
2594 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002595 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002596 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002598 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 }
2600 else
2601 {
2602 cw = 1;
2603 l = 1;
2604 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002605
Bram Moolenaar85a20022019-12-21 18:25:54 +01002606 // When drawing from right to left or when a double-wide character
2607 // doesn't fit, draw a single character here. Otherwise collect
2608 // characters and draw them all at once later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 if (
2610# ifdef FEAT_RIGHTLEFT
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002611 cmdmsg_rl ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002613 (cw > 1 && msg_col + t_col >= Columns - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 if (l > 1)
2616 s = screen_puts_mbyte(s, l, attr) - 1;
2617 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 msg_screen_putchar(*s, attr);
2619 }
2620 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002622 // postpone this character until later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 if (t_col == 0)
2624 t_s = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 t_col += cw;
2626 s += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 }
2628 }
2629 ++s;
2630 }
2631
Bram Moolenaar85a20022019-12-21 18:25:54 +01002632 // output any postponed text
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 if (t_col > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002634 {
2635#ifdef HAS_MESSAGE_WINDOW
2636 if (msg_win != NULL)
2637 put_msg_win(msg_win, where, t_s, s, lnum);
2638 else
2639#endif
2640 t_puts(&t_col, t_s, s, attr);
2641 }
2642
2643#ifdef HAS_MESSAGE_WINDOW
2644 if (msg_win != NULL)
2645 popup_show_message_win();
2646#endif
Bram Moolenaar11901392022-09-26 19:50:44 +01002647 // Store the text for scroll back, unless it's a newline by itself.
2648 if (p_more && !recurse && !(s == sb_str + 1 && *sb_str == '\n'))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002649 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650
2651 msg_check();
2652}
2653
2654/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002655 * Return TRUE when ":filter pattern" was used and "msg" does not match
2656 * "pattern".
2657 */
2658 int
2659message_filtered(char_u *msg)
2660{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002661 int match;
2662
Bram Moolenaare1004402020-10-24 20:49:43 +02002663 if (cmdmod.cmod_filter_regmatch.regprog == NULL)
Bram Moolenaard29459b2016-08-26 22:29:11 +02002664 return FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02002665 match = vim_regexec(&cmdmod.cmod_filter_regmatch, msg, (colnr_T)0);
2666 return cmdmod.cmod_filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002667}
2668
2669/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002670 * Scroll the screen up one line for displaying the next message line.
2671 */
2672 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002673msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002674{
Bram Moolenaar9198de32022-08-27 21:30:03 +01002675#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +01002676 if (in_echowindow)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002677 return;
2678#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002679#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01002680 // Remove the cursor before scrolling, ScreenLines[] is going
2681 // to become invalid.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002682 if (gui.in_use)
2683 gui_undraw_cursor();
2684#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002685 // scrolling up always works
Bram Moolenaara338adc2018-01-31 20:51:47 +01002686 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002687 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002688 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002689
2690 if (!can_clear((char_u *)" "))
2691 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002692 // Scrolling up doesn't result in the right background. Set the
2693 // background here. It's not efficient, but avoids that we have to do
2694 // it all over the code.
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02002695 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns,
2696 ' ', ' ', HL_ATTR(HLF_MSG));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002697
Bram Moolenaar85a20022019-12-21 18:25:54 +01002698 // Also clear the last char of the last but one line if it was not
2699 // cleared before to avoid a scroll-up.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002700 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2701 screen_fill((int)Rows - 2, (int)Rows - 1,
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02002702 (int)Columns - 1, (int)Columns,
2703 ' ', ' ', HL_ATTR(HLF_MSG));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002704 }
2705}
2706
2707/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002708 * Increment "msg_scrolled".
2709 */
2710 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002711inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002712{
2713#ifdef FEAT_EVAL
2714 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2715 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002716 char_u *p = SOURCING_NAME;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002717 char_u *tofree = NULL;
2718 int len;
2719
Bram Moolenaar85a20022019-12-21 18:25:54 +01002720 // v:scrollstart is empty, set it to the script/function name and line
2721 // number
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002722 if (p == NULL)
2723 p = (char_u *)_("Unknown");
2724 else
2725 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002726 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002727 tofree = alloc(len);
2728 if (tofree != NULL)
2729 {
2730 vim_snprintf((char *)tofree, len, _("%s line %ld"),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002731 p, (long)SOURCING_LNUM);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002732 p = tofree;
2733 }
2734 }
2735 set_vim_var_string(VV_SCROLLSTART, p, -1);
2736 vim_free(tofree);
2737 }
2738#endif
2739 ++msg_scrolled;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01002740 set_must_redraw(UPD_VALID);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002741}
2742
2743/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002744 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2745 * store the displayed text and remember where screen lines start.
2746 */
2747typedef struct msgchunk_S msgchunk_T;
2748struct msgchunk_S
2749{
2750 msgchunk_T *sb_next;
2751 msgchunk_T *sb_prev;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002752 char sb_eol; // TRUE when line ends after this text
2753 int sb_msg_col; // column in which text starts
2754 int sb_attr; // text attributes
2755 char_u sb_text[1]; // text to be displayed, actually longer
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002756};
2757
Bram Moolenaar85a20022019-12-21 18:25:54 +01002758static msgchunk_T *last_msgchunk = NULL; // last displayed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002759
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002760static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002761
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002762typedef enum {
2763 SB_CLEAR_NONE = 0,
2764 SB_CLEAR_ALL,
2765 SB_CLEAR_CMDLINE_BUSY,
2766 SB_CLEAR_CMDLINE_DONE
2767} sb_clear_T;
2768
Bram Moolenaar85a20022019-12-21 18:25:54 +01002769// When to clear text on next msg.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002770static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002771
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002772/*
2773 * Store part of a printed message for displaying when scrolling back.
2774 */
2775 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002776store_sb_text(
Bram Moolenaar85a20022019-12-21 18:25:54 +01002777 char_u **sb_str, // start of string
2778 char_u *s, // just after string
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002779 int attr,
2780 int *sb_col,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002781 int finish) // line ends
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002782{
2783 msgchunk_T *mp;
2784
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002785 if (do_clear_sb_text == SB_CLEAR_ALL
2786 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002787 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002788 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
zeertzjq46af7bc2022-07-28 12:34:09 +01002789 msg_sb_eol(); // prevent messages from overlapping
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002790 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002791 }
2792
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002793 if (s > *sb_str)
2794 {
zeertzjq1b438a82023-02-01 13:11:15 +00002795 mp = alloc(offsetof(msgchunk_T, sb_text) + (s - *sb_str) + 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002796 if (mp != NULL)
2797 {
2798 mp->sb_eol = finish;
2799 mp->sb_msg_col = *sb_col;
2800 mp->sb_attr = attr;
2801 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2802
2803 if (last_msgchunk == NULL)
2804 {
2805 last_msgchunk = mp;
2806 mp->sb_prev = NULL;
2807 }
2808 else
2809 {
2810 mp->sb_prev = last_msgchunk;
2811 last_msgchunk->sb_next = mp;
2812 last_msgchunk = mp;
2813 }
2814 mp->sb_next = NULL;
2815 }
2816 }
2817 else if (finish && last_msgchunk != NULL)
2818 last_msgchunk->sb_eol = TRUE;
2819
2820 *sb_str = s;
2821 *sb_col = 0;
2822}
2823
2824/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002825 * Finished showing messages, clear the scroll-back text on the next message.
2826 */
2827 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002828may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002829{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002830 do_clear_sb_text = SB_CLEAR_ALL;
2831}
2832
2833/*
zeertzjq46af7bc2022-07-28 12:34:09 +01002834 * Starting to edit the command line: do not clear messages now.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002835 */
2836 void
2837sb_text_start_cmdline(void)
2838{
zeertzjq46af7bc2022-07-28 12:34:09 +01002839 if (do_clear_sb_text == SB_CLEAR_CMDLINE_BUSY)
2840 // Invoking command line recursively: the previous-level command line
2841 // doesn't need to be remembered as it will be redrawn when returning
2842 // to that level.
2843 sb_text_restart_cmdline();
2844 else
2845 {
2846 msg_sb_eol();
2847 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2848 }
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002849}
2850
2851/*
zeertzjq46af7bc2022-07-28 12:34:09 +01002852 * Redrawing the command line: clear the last unfinished line.
2853 */
2854 void
2855sb_text_restart_cmdline(void)
2856{
2857 msgchunk_T *tofree;
2858
2859 // Needed when returning from nested command line.
2860 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2861
2862 if (last_msgchunk == NULL || last_msgchunk->sb_eol)
2863 // No unfinished line: don't clear anything.
2864 return;
2865
2866 tofree = msg_sb_start(last_msgchunk);
2867 last_msgchunk = tofree->sb_prev;
2868 if (last_msgchunk != NULL)
2869 last_msgchunk->sb_next = NULL;
2870 while (tofree != NULL)
2871 {
2872 msgchunk_T *tofree_next = tofree->sb_next;
2873
2874 vim_free(tofree);
2875 tofree = tofree_next;
2876 }
2877}
2878
2879/*
2880 * Ending to edit the command line: clear old lines but the last one later.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002881 */
2882 void
2883sb_text_end_cmdline(void)
2884{
2885 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002886}
2887
2888/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002889 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002890 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002891 * Called when redrawing the screen.
2892 */
2893 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002894clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002895{
2896 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002897 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002898
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002899 if (all)
2900 lastp = &last_msgchunk;
2901 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002902 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002903 if (last_msgchunk == NULL)
2904 return;
zeertzjqecdc82e2022-07-25 19:50:57 +01002905 lastp = &msg_sb_start(last_msgchunk)->sb_prev;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002906 }
2907
2908 while (*lastp != NULL)
2909 {
2910 mp = (*lastp)->sb_prev;
2911 vim_free(*lastp);
2912 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002913 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002914}
2915
2916/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002917 * "g<" command.
2918 */
2919 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002920show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002921{
2922 msgchunk_T *mp;
2923
Bram Moolenaar85a20022019-12-21 18:25:54 +01002924 // Only show something if there is more than one line, otherwise it looks
2925 // weird, typing a command without output results in one line.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002926 mp = msg_sb_start(last_msgchunk);
2927 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002928 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002929 else
2930 {
2931 do_more_prompt('G');
2932 wait_return(FALSE);
2933 }
2934}
2935
2936/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002937 * Move to the start of screen line in already displayed text.
2938 */
2939 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002940msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002941{
2942 msgchunk_T *mp = mps;
2943
2944 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2945 mp = mp->sb_prev;
2946 return mp;
2947}
2948
2949/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002950 * Mark the last message chunk as finishing the line.
2951 */
2952 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002953msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002954{
2955 if (last_msgchunk != NULL)
2956 last_msgchunk->sb_eol = TRUE;
2957}
2958
2959/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002960 * Display a screen line from previously displayed text at row "row".
Bram Moolenaar838b7462022-09-26 15:19:56 +01002961 * When "clear_to_eol" is set clear the rest of the screen line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002962 * Returns a pointer to the text for the next line (can be NULL).
2963 */
2964 static msgchunk_T *
Bram Moolenaar838b7462022-09-26 15:19:56 +01002965disp_sb_line(int row, msgchunk_T *smp, int clear_to_eol)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002966{
2967 msgchunk_T *mp = smp;
2968 char_u *p;
2969
2970 for (;;)
2971 {
2972 msg_row = row;
2973 msg_col = mp->sb_msg_col;
2974 p = mp->sb_text;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002975 if (*p == '\n') // don't display the line break
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002976 ++p;
2977 msg_puts_display(p, -1, mp->sb_attr, TRUE);
Bram Moolenaar838b7462022-09-26 15:19:56 +01002978
2979 // If clearing the screen did not work (e.g. because of a background
2980 // color and t_ut isn't set) clear until the last column here.
2981 if (clear_to_eol)
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02002982 screen_fill(row, row + 1, msg_col, (int)Columns,
2983 ' ', ' ', HL_ATTR(HLF_MSG));
Bram Moolenaar838b7462022-09-26 15:19:56 +01002984
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002985 if (mp->sb_eol || mp->sb_next == NULL)
2986 break;
2987 mp = mp->sb_next;
2988 }
2989 return mp->sb_next;
2990}
2991
2992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 * Output any postponed text for msg_puts_attr_len().
2994 */
2995 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002996t_puts(
2997 int *t_col,
2998 char_u *t_s,
2999 char_u *s,
3000 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003002 // output postponed text
3003 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003005 msg_col += *t_col;
3006 *t_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003007 // If the string starts with a composing character don't increment the
3008 // column position for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
3010 --msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 if (msg_col >= Columns)
3012 {
3013 msg_col = 0;
3014 ++msg_row;
3015 }
3016}
3017
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018/*
3019 * Returns TRUE when messages should be printed with mch_errmsg().
3020 * This is used when there is no valid screen, so we can see error messages.
3021 * If termcap is not active, we may be writing in an alternate console
3022 * window, cursor positioning may not work correctly (window size may be
3023 * different, e.g. for Win32 console) or we just don't know where the
3024 * cursor is.
3025 */
3026 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003027msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028{
3029 return (!msg_check_screen()
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003030#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3031# ifdef VIMDLL
3032 || (!gui.in_use && !termcap_active)
3033# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 || !termcap_active
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003035# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036#endif
3037 || (swapping_screen() && !termcap_active)
3038 );
3039}
3040
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003041/*
3042 * Print a message when there is no valid screen.
3043 */
3044 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003045msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003046{
3047 char_u *s = str;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003048 char_u *buf = NULL;
3049 char_u *p = s;
3050
Bram Moolenaar4f974752019-02-17 17:44:42 +01003051#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003052 if (!(silent_mode && p_verbose == 0))
Bram Moolenaar85a20022019-12-21 18:25:54 +01003053 mch_settmode(TMODE_COOK); // handle CR and NL correctly
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003054#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02003055 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003056 {
3057 if (!(silent_mode && p_verbose == 0))
3058 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003059 // NL --> CR NL translation (for Unix, not for "--version")
3060 if (*s == NL)
3061 {
3062 int n = (int)(s - p);
3063
3064 buf = alloc(n + 3);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02003065 if (buf != NULL)
3066 {
3067 memcpy(buf, p, n);
3068 if (!info_message)
3069 buf[n++] = CAR;
3070 buf[n++] = NL;
3071 buf[n++] = NUL;
3072 if (info_message) // informative message, not an error
3073 mch_msg((char *)buf);
3074 else
3075 mch_errmsg((char *)buf);
3076 vim_free(buf);
3077 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003078 p = s + 1;
3079 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003080 }
3081
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003082 // primitive way to compute the current column
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003083#ifdef FEAT_RIGHTLEFT
3084 if (cmdmsg_rl)
3085 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003086 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003087 msg_col = Columns - 1;
3088 else
3089 --msg_col;
3090 }
3091 else
3092#endif
3093 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003094 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003095 msg_col = 0;
3096 else
3097 ++msg_col;
3098 }
3099 ++s;
3100 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003101
3102 if (*p != NUL && !(silent_mode && p_verbose == 0))
3103 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02003104 char_u *tofree = NULL;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003105
Bram Moolenaarc32949b2023-01-04 15:56:51 +00003106 if (maxlen > 0 && vim_strlen_maxlen((char *)p, (size_t)maxlen)
3107 >= (size_t)maxlen)
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003108 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02003109 tofree = vim_strnsave(p, (size_t)maxlen);
3110 p = tofree;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003111 }
Bram Moolenaara97c3632021-06-15 22:39:11 +02003112 if (p != NULL)
3113 {
3114 if (info_message)
3115 mch_msg((char *)p);
3116 else
3117 mch_errmsg((char *)p);
3118 vim_free(tofree);
3119 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003120 }
3121
3122 msg_didout = TRUE; // assume that line is not empty
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003123
Bram Moolenaar4f974752019-02-17 17:44:42 +01003124#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003125 if (!(silent_mode && p_verbose == 0))
3126 mch_settmode(TMODE_RAW);
3127#endif
3128}
3129
3130/*
3131 * Show the more-prompt and handle the user response.
3132 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003133 * When at hit-enter prompt "typed_char" is the already typed character,
3134 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003135 * Returns TRUE when jumping ahead to "confirm_msg_tail".
3136 */
3137 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003138do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003139{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003140 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003141 int used_typed_char = typed_char;
3142 int oldState = State;
3143 int c;
3144#ifdef FEAT_CON_DIALOG
3145 int retval = FALSE;
3146#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003147 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003148 msgchunk_T *mp_last = NULL;
3149 msgchunk_T *mp;
3150 int i;
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003151 int msg_attr;
3152
3153 msg_attr = HL_ATTR(HLF_MSG);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003154
Bram Moolenaar85a20022019-12-21 18:25:54 +01003155 // We get called recursively when a timer callback outputs a message. In
3156 // that case don't show another prompt. Also when at the hit-Enter prompt
3157 // and nothing was typed.
Bram Moolenaar24959102022-05-07 20:01:16 +01003158 if (entered || (State == MODE_HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003159 return FALSE;
3160 entered = TRUE;
3161
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003162 if (typed_char == 'G')
3163 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003164 // "g<": Find first line on the last page.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003165 mp_last = msg_sb_start(last_msgchunk);
3166 for (i = 0; i < Rows - 2 && mp_last != NULL
3167 && mp_last->sb_prev != NULL; ++i)
3168 mp_last = msg_sb_start(mp_last->sb_prev);
3169 }
3170
Bram Moolenaar24959102022-05-07 20:01:16 +01003171 State = MODE_ASKMORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003172 setmouse();
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003173 if (typed_char == NUL)
3174 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003175 for (;;)
3176 {
3177 /*
3178 * Get a typed character directly from the user.
3179 */
3180 if (used_typed_char != NUL)
3181 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003182 c = used_typed_char; // was typed at hit-enter prompt
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003183 used_typed_char = NUL;
3184 }
3185 else
3186 c = get_keystroke();
3187
3188#if defined(FEAT_MENU) && defined(FEAT_GUI)
3189 if (c == K_MENU)
3190 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003191 int idx = get_menu_index(current_menu, MODE_ASKMORE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003192
Bram Moolenaar85a20022019-12-21 18:25:54 +01003193 // Used a menu. If it starts with CTRL-Y, it must
3194 // be a "Copy" for the clipboard. Otherwise
3195 // assume that we end
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003196 if (idx == MENU_INDEX_INVALID)
3197 continue;
3198 c = *current_menu->strings[idx];
3199 if (c != NUL && current_menu->strings[idx][1] != NUL)
3200 ins_typebuf(current_menu->strings[idx] + 1,
3201 current_menu->noremap[idx], 0, TRUE,
3202 current_menu->silent[idx]);
3203 }
3204#endif
3205
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003206 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003207 switch (c)
3208 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003209 case BS: // scroll one line back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003210 case K_BS:
3211 case 'k':
3212 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003213 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003214 break;
3215
Bram Moolenaar85a20022019-12-21 18:25:54 +01003216 case CAR: // one extra line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003217 case NL:
3218 case 'j':
3219 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003220 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003221 break;
3222
Bram Moolenaar85a20022019-12-21 18:25:54 +01003223 case 'u': // Up half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003224 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003225 break;
3226
Bram Moolenaar85a20022019-12-21 18:25:54 +01003227 case 'd': // Down half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003228 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003229 break;
3230
Bram Moolenaar85a20022019-12-21 18:25:54 +01003231 case 'b': // one page back
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00003232 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003233 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003234 break;
3235
Bram Moolenaar85a20022019-12-21 18:25:54 +01003236 case ' ': // one extra page
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00003237 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003238 case K_PAGEDOWN:
3239 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003240 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003241 break;
3242
Bram Moolenaar85a20022019-12-21 18:25:54 +01003243 case 'g': // all the way back to the start
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003244 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003245 break;
3246
Bram Moolenaar85a20022019-12-21 18:25:54 +01003247 case 'G': // all the way to the end
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003248 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003249 lines_left = 999999;
3250 break;
3251
Bram Moolenaar85a20022019-12-21 18:25:54 +01003252 case ':': // start new command line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003253#ifdef FEAT_CON_DIALOG
3254 if (!confirm_msg_used)
3255#endif
3256 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003257 // Since got_int is set all typeahead will be flushed, but we
3258 // want to keep this ':', remember that in a special way.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003259 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02003260#ifdef FEAT_TERMINAL
3261 skip_term_loop = TRUE;
3262#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003263 cmdline_row = Rows - 1; // put ':' on this line
3264 skip_redraw = TRUE; // skip redraw once
3265 need_wait_return = FALSE; // don't wait in main()
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003266 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003267 // FALLTHROUGH
3268 case 'q': // quit
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003269 case Ctrl_C:
3270 case ESC:
3271#ifdef FEAT_CON_DIALOG
3272 if (confirm_msg_used)
3273 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003274 // Jump to the choices of the dialog.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003275 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003276 }
3277 else
3278#endif
3279 {
3280 got_int = TRUE;
3281 quit_more = TRUE;
3282 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003283 // When there is some more output (wrapping line) display that
3284 // without another prompt.
Bram Moolenaar51306d22009-02-24 03:38:04 +00003285 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003286 break;
3287
3288#ifdef FEAT_CLIPBOARD
3289 case Ctrl_Y:
Bram Moolenaar85a20022019-12-21 18:25:54 +01003290 // Strange way to allow copying (yanking) a modeless
3291 // selection at the more prompt. Use CTRL-Y,
3292 // because the same is used in Cmdline-mode and at the
3293 // hit-enter prompt. However, scrolling one line up
3294 // might be expected...
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003295 if (clip_star.state == SELECT_DONE)
3296 clip_copy_modeless_selection(TRUE);
3297 continue;
3298#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003299 default: // no valid response
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003300 msg_moremsg(TRUE);
3301 continue;
3302 }
3303
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003304 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003305 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003306 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003307 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003308 // go to start of last line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003309 if (mp_last == NULL)
3310 mp = msg_sb_start(last_msgchunk);
3311 else if (mp_last->sb_prev != NULL)
3312 mp = msg_sb_start(mp_last->sb_prev);
3313 else
3314 mp = NULL;
3315
Bram Moolenaar85a20022019-12-21 18:25:54 +01003316 // go to start of line at top of the screen
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003317 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
3318 ++i)
3319 mp = msg_sb_start(mp->sb_prev);
3320
3321 if (mp != NULL && mp->sb_prev != NULL)
3322 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003323 // Find line to be displayed at top.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003324 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003325 {
3326 if (mp == NULL || mp->sb_prev == NULL)
3327 break;
3328 mp = msg_sb_start(mp->sb_prev);
3329 if (mp_last == NULL)
3330 mp_last = msg_sb_start(last_msgchunk);
3331 else
3332 mp_last = msg_sb_start(mp_last->sb_prev);
3333 }
3334
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003335 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003336 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003337 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003338 // display line at top
Bram Moolenaar838b7462022-09-26 15:19:56 +01003339 (void)disp_sb_line(0, mp, FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003340 }
3341 else
3342 {
Bram Moolenaar838b7462022-09-26 15:19:56 +01003343 int did_clear = screenclear();
3344
Bram Moolenaar85a20022019-12-21 18:25:54 +01003345 // redisplay all lines
Bram Moolenaar773560b2006-05-06 21:38:18 +00003346 for (i = 0; mp != NULL && i < Rows - 1; ++i)
3347 {
Bram Moolenaar838b7462022-09-26 15:19:56 +01003348 mp = disp_sb_line(i, mp, !did_clear);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003349 ++msg_scrolled;
3350 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003351 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003352 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003353 }
3354 }
3355 else
3356 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003357 // First display any text that we scrolled back.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003358 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003359 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003360 // scroll up, display line at bottom
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003361 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00003362 inc_msg_scrolled();
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003363 screen_fill((int)Rows - 2, (int)Rows - 1, 0, (int)Columns,
3364 ' ', ' ', msg_attr);
Bram Moolenaar838b7462022-09-26 15:19:56 +01003365 mp_last = disp_sb_line((int)Rows - 2, mp_last, FALSE);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003366 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003367 }
3368 }
3369
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003370 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003371 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003372 // displayed the requested text, more prompt again
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003373 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns,
3374 ' ', ' ', msg_attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003375 msg_moremsg(FALSE);
3376 continue;
3377 }
3378
Bram Moolenaar85a20022019-12-21 18:25:54 +01003379 // display more text, return to caller
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003380 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003381 }
3382
3383 break;
3384 }
3385
Bram Moolenaar85a20022019-12-21 18:25:54 +01003386 // clear the --more-- message
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003387 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', msg_attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003388 State = oldState;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003389 setmouse();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003390 if (quit_more)
3391 {
3392 msg_row = Rows - 1;
3393 msg_col = 0;
3394 }
3395#ifdef FEAT_RIGHTLEFT
3396 else if (cmdmsg_rl)
3397 msg_col = Columns - 1;
3398#endif
3399
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003400 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003401#ifdef FEAT_CON_DIALOG
3402 return retval;
3403#else
3404 return FALSE;
3405#endif
3406}
3407
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408#if defined(USE_MCH_ERRMSG) || defined(PROTO)
3409
3410#ifdef mch_errmsg
3411# undef mch_errmsg
3412#endif
3413#ifdef mch_msg
3414# undef mch_msg
3415#endif
3416
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003417#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3418 static void
3419mch_errmsg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003421 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003422 DWORD nwrite = 0;
3423 DWORD mode = 0;
3424 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
3425
3426 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3427 && (int)GetConsoleCP() != enc_codepage)
3428 {
3429 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3430
3431 WriteConsoleW(h, w, len, &nwrite, NULL);
3432 vim_free(w);
3433 }
3434 else
3435 {
3436 fprintf(stderr, "%s", str);
3437 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003438}
3439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003441/*
3442 * Give an error message. To be used when the screen hasn't been initialized
3443 * yet. When stderr can't be used, collect error messages until the GUI has
3444 * started and they can be displayed in a message box.
3445 */
3446 void
3447mch_errmsg(char *str)
3448{
3449#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
3450 int len;
3451#endif
3452
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003453#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003454 // On Unix use stderr if it's a tty.
3455 // When not going to start the GUI also use stderr.
3456 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003458# ifdef UNIX
3459# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003461# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 isatty(2)
3463# endif
3464# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003465 ||
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003466# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003467# endif
3468# ifdef FEAT_GUI
3469 !(gui.in_use || gui.starting)
3470# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 )
3472 {
3473 fprintf(stderr, "%s", str);
3474 return;
3475 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003478#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3479# ifdef VIMDLL
3480 if (!(gui.in_use || gui.starting))
3481# endif
3482 {
3483 mch_errmsg_c(str);
3484 return;
3485 }
3486#endif
3487
3488#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003489 // avoid a delay for a message that isn't there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 emsg_on_display = FALSE;
3491
3492 len = (int)STRLEN(str) + 1;
3493 if (error_ga.ga_growsize == 0)
3494 {
3495 error_ga.ga_growsize = 80;
3496 error_ga.ga_itemsize = 1;
3497 }
3498 if (ga_grow(&error_ga, len) == OK)
3499 {
3500 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3501 (char_u *)str, len);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003502# ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01003503 // remove CR characters, they are displayed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 {
3505 char_u *p;
3506
3507 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3508 for (;;)
3509 {
3510 p = vim_strchr(p, '\r');
3511 if (p == NULL)
3512 break;
3513 *p = ' ';
3514 }
3515 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003516# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003517 --len; // don't count the NUL at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521}
3522
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003523#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3524 static void
3525mch_msg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003527 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003528 DWORD nwrite = 0;
3529 DWORD mode;
3530 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
3531
3532
3533 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3534 && (int)GetConsoleCP() != enc_codepage)
3535 {
3536 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3537
3538 WriteConsoleW(h, w, len, &nwrite, NULL);
3539 vim_free(w);
3540 }
3541 else
3542 {
3543 printf("%s", str);
3544 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003545}
3546#endif
3547
3548/*
3549 * Give a message. To be used when the screen hasn't been initialized yet.
3550 * When there is no tty, collect messages until the GUI has started and they
3551 * can be displayed in a message box.
3552 */
3553 void
3554mch_msg(char *str)
3555{
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003556#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003557 // On Unix use stdout if we have a tty. This allows "vim -h | more" and
3558 // uses mch_errmsg() when started from the desktop.
3559 // When not going to start the GUI also use stdout.
3560 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003562# ifdef UNIX
3563# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003565# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 isatty(2)
Bram Moolenaareae1b912019-05-09 15:12:55 +02003567# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003569 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003571# endif
3572# ifdef FEAT_GUI
3573 !(gui.in_use || gui.starting)
3574# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 )
3576 {
3577 printf("%s", str);
3578 return;
3579 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003580#endif
3581
3582#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3583# ifdef VIMDLL
3584 if (!(gui.in_use || gui.starting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003586 {
3587 mch_msg_c(str);
3588 return;
3589 }
3590#endif
3591#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 mch_errmsg(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594}
Bram Moolenaar85a20022019-12-21 18:25:54 +01003595#endif // USE_MCH_ERRMSG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596
3597/*
3598 * Put a character on the screen at the current message position and advance
3599 * to the next position. Only for printable ASCII!
3600 */
3601 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003602msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003604 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 screen_putchar(c, msg_row, msg_col, attr);
3606#ifdef FEAT_RIGHTLEFT
3607 if (cmdmsg_rl)
3608 {
3609 if (--msg_col == 0)
3610 {
3611 msg_col = Columns;
3612 ++msg_row;
3613 }
3614 }
3615 else
3616#endif
3617 {
3618 if (++msg_col >= Columns)
3619 {
3620 msg_col = 0;
3621 ++msg_row;
3622 }
3623 }
3624}
3625
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003626 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003627msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003629 int attr;
3630 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631
Bram Moolenaar8820b482017-03-16 17:23:31 +01003632 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003633 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003635 screen_puts((char_u *)
3636 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3637 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638}
3639
3640/*
Bram Moolenaar24959102022-05-07 20:01:16 +01003641 * Repeat the message for the current mode: MODE_ASKMORE, MODE_EXTERNCMD,
3642 * MODE_CONFIRM or exmode_active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 */
3644 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003645repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646{
Bram Moolenaar24959102022-05-07 20:01:16 +01003647 if (State == MODE_ASKMORE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003649 msg_moremsg(TRUE); // display --more-- message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 msg_row = Rows - 1;
3651 }
3652#ifdef FEAT_CON_DIALOG
Bram Moolenaar24959102022-05-07 20:01:16 +01003653 else if (State == MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003655 display_confirm_msg(); // display ":confirm" message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 msg_row = Rows - 1;
3657 }
3658#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003659 else if (State == MODE_EXTERNCMD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003661 windgoto(msg_row, msg_col); // put cursor back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003663 else if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003665 if (msg_row == Rows - 1)
3666 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003667 // Avoid drawing the "hit-enter" prompt below the previous one,
3668 // overwrite it. Esp. useful when regaining focus and a
3669 // FocusGained autocmd exists but didn't draw anything.
Bram Moolenaar9f108752007-11-24 14:44:58 +00003670 msg_didout = FALSE;
3671 msg_col = 0;
3672 msg_clr_eos();
3673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 hit_return_msg();
3675 msg_row = Rows - 1;
3676 }
3677}
3678
3679/*
3680 * msg_check_screen - check if the screen is initialized.
3681 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3682 * While starting the GUI the terminal codes will be set for the GUI, but the
3683 * output goes to the terminal. Don't use the terminal codes then.
3684 */
3685 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003686msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687{
3688 if (!full_screen || !screen_valid(FALSE))
3689 return FALSE;
3690
3691 if (msg_row >= Rows)
3692 msg_row = Rows - 1;
3693 if (msg_col >= Columns)
3694 msg_col = Columns - 1;
3695 return TRUE;
3696}
3697
3698/*
3699 * Clear from current message position to end of screen.
3700 * Skip this when ":silent" was used, no need to clear for redirection.
3701 */
3702 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003703msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704{
3705 if (msg_silent == 0)
3706 msg_clr_eos_force();
3707}
3708
3709/*
3710 * Clear from current message position to end of screen.
3711 * Note: msg_col is not updated, so we remember the end of the message
3712 * for msg_check().
3713 */
3714 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003715msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716{
Bram Moolenaar3b474dc2022-09-01 17:01:32 +01003717#ifdef HAS_MESSAGE_WINDOW
3718 if (in_echowindow)
3719 return; // messages go into a popup
3720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 if (msg_use_printf())
3722 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003723 if (full_screen) // only when termcap codes are valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 {
3725 if (*T_CD)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003726 out_str(T_CD); // clear to end of display
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 else if (*T_CE)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003728 out_str(T_CE); // clear to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 }
3730 }
Bram Moolenaara2a89732022-08-31 14:46:18 +01003731 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 {
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003733 int msg_attr;
3734
3735 msg_attr = HL_ATTR(HLF_MSG);
3736
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737#ifdef FEAT_RIGHTLEFT
3738 if (cmdmsg_rl)
3739 {
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003740 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1,
3741 ' ', ' ', msg_attr);
3742 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns,
3743 ' ', ' ', msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 }
3745 else
3746#endif
3747 {
3748 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003749 ' ', ' ', msg_attr);
3750 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns,
3751 ' ', ' ', msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 }
3753 }
3754}
3755
3756/*
3757 * Clear the command line.
3758 */
3759 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003760msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761{
3762 msg_row = cmdline_row;
3763 msg_col = 0;
3764 msg_clr_eos_force();
3765}
3766
3767/*
3768 * end putting a message on the screen
Bram Moolenaar13608d82022-08-29 15:06:50 +01003769 * call wait_return() if the message does not fit in the available space
3770 * return TRUE if wait_return() not called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 */
3772 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003773msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774{
3775 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003776 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 * or the ruler option is set and we run into it,
3778 * we have to redraw the window.
3779 * Do not do this if we are abandoning the file or editing the command line.
3780 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003781 if (!exiting && need_wait_return && !(State & MODE_CMDLINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 {
3783 wait_return(FALSE);
3784 return FALSE;
3785 }
3786 out_flush();
3787 return TRUE;
3788}
3789
3790/*
3791 * If the written message runs into the shown command or ruler, we have to
3792 * wait for hit-return and redraw the window later.
3793 */
3794 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003795msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796{
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01003797 if (msg_row == Rows - 1 && msg_col >= sc_col
3798#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +01003799 && !in_echowindow
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01003800#endif
3801 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 {
3803 need_wait_return = TRUE;
3804 redraw_cmdline = TRUE;
3805 }
3806}
3807
3808/*
3809 * May write a string to the redirection file.
3810 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3811 */
3812 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003813redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814{
3815 char_u *s = str;
3816 static int cur_col = 0;
3817
Bram Moolenaar85a20022019-12-21 18:25:54 +01003818 // Don't do anything for displaying prompts and the like.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003819 if (redir_off)
3820 return;
3821
Bram Moolenaar85a20022019-12-21 18:25:54 +01003822 // If 'verbosefile' is set prepare for writing in that file.
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003823 if (*p_vfile != NUL && verbose_fd == NULL)
3824 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003825
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003826 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003828 // If the string doesn't start with CR or NL, go to msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 if (*s != '\n' && *s != '\r')
3830 {
3831 while (cur_col < msg_col)
3832 {
3833#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003834 if (redir_execute)
3835 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003836 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003838 else if (redir_vname)
3839 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003840 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003842 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003844 if (verbose_fd != NULL)
3845 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 ++cur_col;
3847 }
3848 }
3849
3850#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003851 if (redir_execute)
3852 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003853 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003855 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003856 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857#endif
3858
Bram Moolenaar85a20022019-12-21 18:25:54 +01003859 // Write and adjust the current column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3861 {
3862#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003863 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003865 if (redir_fd != NULL)
3866 putc(*s, redir_fd);
3867 if (verbose_fd != NULL)
3868 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 if (*s == '\r' || *s == '\n')
3870 cur_col = 0;
3871 else if (*s == '\t')
3872 cur_col += (8 - cur_col % 8);
3873 else
3874 ++cur_col;
3875 ++s;
3876 }
3877
Bram Moolenaar85a20022019-12-21 18:25:54 +01003878 if (msg_silent != 0) // should update msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 msg_col = cur_col;
3880 }
3881}
3882
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003883 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003884redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003885{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003886 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003887#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003888 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003889#endif
3890 ;
3891}
3892
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003894 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003895 * Must always be called paired with verbose_leave()!
3896 */
3897 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003898verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003899{
3900 if (*p_vfile != NUL)
3901 ++msg_silent;
3902}
3903
3904/*
3905 * After giving verbose message.
3906 * Must always be called paired with verbose_enter()!
3907 */
3908 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003909verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003910{
3911 if (*p_vfile != NUL)
3912 if (--msg_silent < 0)
3913 msg_silent = 0;
3914}
3915
3916/*
3917 * Like verbose_enter() and set msg_scroll when displaying the message.
3918 */
3919 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003920verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003921{
3922 if (*p_vfile != NUL)
3923 ++msg_silent;
3924 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003925 // always scroll up, don't overwrite
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003926 msg_scroll = TRUE;
3927}
3928
3929/*
3930 * Like verbose_leave() and set cmdline_row when displaying the message.
3931 */
3932 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003933verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003934{
3935 if (*p_vfile != NUL)
3936 {
3937 if (--msg_silent < 0)
3938 msg_silent = 0;
3939 }
3940 else
3941 cmdline_row = msg_row;
3942}
3943
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003944/*
3945 * Called when 'verbosefile' is set: stop writing to the file.
3946 */
3947 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003948verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003949{
3950 if (verbose_fd != NULL)
3951 {
3952 fclose(verbose_fd);
3953 verbose_fd = NULL;
3954 }
3955 verbose_did_open = FALSE;
3956}
3957
3958/*
3959 * Open the file 'verbosefile'.
3960 * Return FAIL or OK.
3961 */
3962 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003963verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003964{
3965 if (verbose_fd == NULL && !verbose_did_open)
3966 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003967 // Only give the error message once.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003968 verbose_did_open = TRUE;
3969
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003970 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003971 if (verbose_fd == NULL)
3972 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003973 semsg(_(e_cant_open_file_str), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003974 return FAIL;
3975 }
3976 }
3977 return OK;
3978}
3979
3980/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 * Give a warning message (for searching).
3982 * Use 'w' highlighting and may repeat the message after redrawing
3983 */
3984 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003985give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986{
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003987 give_warning_with_source(message, hl, FALSE);
3988}
3989
3990 void
3991give_warning_with_source(char_u *message, int hl, int with_source)
3992{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003993 // Don't do this for ":silent".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 if (msg_silent != 0)
3995 return;
3996
Bram Moolenaar85a20022019-12-21 18:25:54 +01003997 // Don't want a hit-enter prompt here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003999
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000#ifdef FEAT_EVAL
4001 set_vim_var_string(VV_WARNINGMSG, message, -1);
4002#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01004003 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004005 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 else
4007 keep_msg_attr = 0;
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02004008
4009 if (with_source)
4010 {
4011 // Do what msg() does, but with a column offset if the warning should
4012 // be after the mode message.
4013 msg_start();
4014 msg_source(HL_ATTR(HLF_W));
4015 msg_puts(" ");
4016 msg_puts_attr((char *)message, HL_ATTR(HLF_W) | MSG_HIST);
4017 msg_clr_eos();
4018 (void)msg_end();
4019 }
4020 else if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004021 set_keep_msg(message, keep_msg_attr);
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02004022
Bram Moolenaar85a20022019-12-21 18:25:54 +01004023 msg_didout = FALSE; // overwrite this message
4024 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00004026
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 --no_wait_return;
4028}
4029
Bram Moolenaar113e1072019-01-20 15:30:40 +01004030#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02004031 void
4032give_warning2(char_u *message, char_u *a1, int hl)
4033{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02004034 if (IObuff == NULL)
4035 {
4036 // Very early in initialisation and already something wrong, just give
4037 // the raw message so the user at least gets a hint.
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004038 give_warning(message, hl);
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02004039 }
4040 else
4041 {
4042 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
4043 give_warning(IObuff, hl);
4044 }
Bram Moolenaarf8be4612017-06-23 20:52:40 +02004045}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004046#endif
Bram Moolenaarf8be4612017-06-23 20:52:40 +02004047
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048/*
4049 * Advance msg cursor to column "col".
4050 */
4051 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004052msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004054 if (msg_silent != 0) // nothing to advance to
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004056 msg_col = col; // for redirection, may fill it up later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 return;
4058 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004059 if (col >= Columns) // not enough room
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004061#ifdef FEAT_RIGHTLEFT
4062 if (cmdmsg_rl)
4063 while (msg_col > Columns - col)
4064 msg_putchar(' ');
4065 else
4066#endif
4067 while (msg_col < col)
4068 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069}
4070
Christian Brabandt45e07042024-11-11 20:52:55 +01004071/*
4072 * Warn about missing Clipboard Support
4073 */
4074 void
4075msg_warn_missing_clipboard(void)
4076{
4077 if (!global_busy && !did_warn_clipboard)
4078 {
Christian Brabandt8b94afc2024-11-17 16:02:41 +01004079#ifdef FEAT_CLIPBOARD
4080 msg(_("W23: Clipboard register not available, using register 0"));
4081#else
4082 msg(_("W24: Clipboard register not available. See :h W24"));
4083#endif
Christian Brabandt45e07042024-11-11 20:52:55 +01004084 did_warn_clipboard = TRUE;
4085 }
4086}
4087
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088#if defined(FEAT_CON_DIALOG) || defined(PROTO)
4089/*
4090 * Used for "confirm()" function, and the :confirm command prefix.
4091 * Versions which haven't got flexible dialogs yet, and console
4092 * versions, get this generic handler which uses the command line.
4093 *
4094 * type = one of:
4095 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
4096 * title = title string (can be NULL for default)
4097 * (neither used in console dialogs at the moment)
4098 *
4099 * Format of the "buttons" string:
4100 * "Button1Name\nButton2Name\nButton3Name"
4101 * The first button should normally be the default/accept
4102 * The second button should be the 'Cancel' button
4103 * Other buttons- use your imagination!
4104 * A '&' in a button name becomes a shortcut, so each '&' should be before a
4105 * different letter.
Rob Pilling8196e942022-02-11 15:12:10 +00004106 *
4107 * Returns 0 if cancelled, otherwise the nth button (1-indexed).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004110do_dialog(
4111 int type UNUSED,
4112 char_u *title UNUSED,
4113 char_u *message,
4114 char_u *buttons,
4115 int dfltbutton,
Bram Moolenaar85a20022019-12-21 18:25:54 +01004116 char_u *textfield UNUSED, // IObuff for inputdialog(), NULL
4117 // otherwise
4118 int ex_cmd) // when TRUE pressing : accepts default and starts
4119 // Ex command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120{
4121 int oldState;
4122 int retval = 0;
4123 char_u *hotkeys;
4124 int c;
4125 int i;
Bram Moolenaar27321db2020-07-06 21:24:57 +02004126 tmode_T save_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127
4128#ifndef NO_CONSOLE
Bram Moolenaar85a20022019-12-21 18:25:54 +01004129 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 if (silent_mode)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004131 return dfltbutton; // return default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132#endif
4133
4134#ifdef FEAT_GUI_DIALOG
Bram Moolenaar85a20022019-12-21 18:25:54 +01004135 // When GUI is running and 'c' not in 'guioptions', use the GUI dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
4137 {
Bram Moolenaar2d12c252022-06-13 21:42:45 +01004138 // --gui-dialog-file: write text to a file
4139 if (gui_dialog_log(title, message))
4140 c = dfltbutton;
4141 else
4142 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004143 textfield, ex_cmd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004144 // avoid a hit-enter prompt without clearing the cmdline
Bram Moolenaar8de49e12009-02-11 17:47:54 +00004145 need_wait_return = FALSE;
4146 emsg_on_display = FALSE;
4147 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
Bram Moolenaar85a20022019-12-21 18:25:54 +01004149 // Flush output to avoid that further messages and redrawing is done
4150 // in the wrong order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 out_flush();
4152 gui_mch_update();
4153
4154 return c;
4155 }
4156#endif
4157
4158 oldState = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01004159 State = MODE_CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161
Bram Moolenaar27321db2020-07-06 21:24:57 +02004162 // Ensure raw mode here.
4163 save_tmode = cur_tmode;
4164 settmode(TMODE_RAW);
4165
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 /*
4167 * Since we wait for a keypress, don't make the
4168 * user press RETURN as well afterwards.
4169 */
4170 ++no_wait_return;
4171 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
4172
4173 if (hotkeys != NULL)
4174 {
4175 for (;;)
4176 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004177 // Get a typed character directly from the user.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 c = get_keystroke();
4179 switch (c)
4180 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004181 case CAR: // User accepts default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 case NL:
4183 retval = dfltbutton;
4184 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004185 case Ctrl_C: // User aborts/cancels
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 case ESC:
4187 retval = 0;
4188 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004189 default: // Could be a hotkey?
4190 if (c < 0) // special keys are ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004192 if (c == ':' && ex_cmd)
4193 {
4194 retval = dfltbutton;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02004195 ins_char_typebuf(':', 0);
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004196 break;
4197 }
4198
Bram Moolenaar85a20022019-12-21 18:25:54 +01004199 // Make the character lowercase, as chars in "hotkeys" are.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 c = MB_TOLOWER(c);
4201 retval = 1;
4202 for (i = 0; hotkeys[i]; ++i)
4203 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 if (has_mbyte)
4205 {
4206 if ((*mb_ptr2char)(hotkeys + i) == c)
4207 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004208 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 }
4210 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 if (hotkeys[i] == c)
4212 break;
4213 ++retval;
4214 }
4215 if (hotkeys[i])
4216 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004217 // No hotkey match, so keep waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 continue;
4219 }
4220 break;
4221 }
4222
4223 vim_free(hotkeys);
4224 }
4225
Bram Moolenaar27321db2020-07-06 21:24:57 +02004226 settmode(save_tmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 State = oldState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 --no_wait_return;
4230 msg_end_prompt();
4231
4232 return retval;
4233}
4234
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235/*
4236 * Copy one character from "*from" to "*to", taking care of multi-byte
4237 * characters. Return the length of the character in bytes.
4238 */
4239 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004240copy_char(
4241 char_u *from,
4242 char_u *to,
Bram Moolenaar85a20022019-12-21 18:25:54 +01004243 int lowercase) // make character lower case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 int len;
4246 int c;
4247
4248 if (has_mbyte)
4249 {
4250 if (lowercase)
4251 {
4252 c = MB_TOLOWER((*mb_ptr2char)(from));
4253 return (*mb_char2bytes)(c, to);
4254 }
4255 else
4256 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004257 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 mch_memmove(to, from, (size_t)len);
4259 return len;
4260 }
4261 }
4262 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 {
4264 if (lowercase)
4265 *to = (char_u)TOLOWER_LOC(*from);
4266 else
4267 *to = *from;
4268 return 1;
4269 }
4270}
4271
4272/*
4273 * Format the dialog string, and display it at the bottom of
4274 * the screen. Return a string of hotkey chars (if defined) for
4275 * each 'button'. If a button has no hotkey defined, the first character of
4276 * the button is used.
4277 * The hotkeys can be multi-byte characters, but without combining chars.
4278 *
4279 * Returns an allocated string with hotkeys, or NULL for error.
4280 */
4281 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004282msg_show_console_dialog(
4283 char_u *message,
4284 char_u *buttons,
4285 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286{
4287 int len = 0;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004288#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004289 int lenhotkey = HOTK_LEN; // count first button
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 char_u *hotk = NULL;
4291 char_u *msgp = NULL;
4292 char_u *hotkp = NULL;
4293 char_u *r;
4294 int copy;
4295#define HAS_HOTKEY_LEN 30
4296 char_u has_hotkey[HAS_HOTKEY_LEN];
Bram Moolenaar85a20022019-12-21 18:25:54 +01004297 int first_hotkey = FALSE; // first char of button is hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 int idx;
4299
4300 has_hotkey[0] = FALSE;
4301
4302 /*
4303 * First loop: compute the size of memory to allocate.
4304 * Second loop: copy to the allocated memory.
4305 */
4306 for (copy = 0; copy <= 1; ++copy)
4307 {
4308 r = buttons;
4309 idx = 0;
4310 while (*r)
4311 {
4312 if (*r == DLG_BUTTON_SEP)
4313 {
4314 if (copy)
4315 {
4316 *msgp++ = ',';
Bram Moolenaar85a20022019-12-21 18:25:54 +01004317 *msgp++ = ' '; // '\n' -> ', '
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318
Bram Moolenaar85a20022019-12-21 18:25:54 +01004319 // advance to next hotkey and set default hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00004321 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00004324 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 if (dfltbutton)
4326 --dfltbutton;
4327
Bram Moolenaar85a20022019-12-21 18:25:54 +01004328 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
4330 first_hotkey = TRUE;
4331 }
4332 else
4333 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004334 len += 3; // '\n' -> ', '; 'x' -> '(x)'
4335 lenhotkey += HOTK_LEN; // each button needs a hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 if (idx < HAS_HOTKEY_LEN - 1)
4337 has_hotkey[++idx] = FALSE;
4338 }
4339 }
4340 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
4341 {
4342 if (*r == DLG_HOTKEY_CHAR)
4343 ++r;
4344 first_hotkey = FALSE;
4345 if (copy)
4346 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004347 if (*r == DLG_HOTKEY_CHAR) // '&&a' -> '&a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 *msgp++ = *r;
4349 else
4350 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004351 // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 *msgp++ = (dfltbutton == 1) ? '[' : '(';
4353 msgp += copy_char(r, msgp, FALSE);
4354 *msgp++ = (dfltbutton == 1) ? ']' : ')';
4355
Bram Moolenaar85a20022019-12-21 18:25:54 +01004356 // redefine hotkey
Bram Moolenaar6a516062007-07-05 08:11:42 +00004357 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 }
4359 }
4360 else
4361 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004362 ++len; // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 if (idx < HAS_HOTKEY_LEN - 1)
4364 has_hotkey[idx] = TRUE;
4365 }
4366 }
4367 else
4368 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004369 // everything else copy literally
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 if (copy)
4371 msgp += copy_char(r, msgp, FALSE);
4372 }
4373
Bram Moolenaar85a20022019-12-21 18:25:54 +01004374 // advance to the next character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004375 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 }
4377
4378 if (copy)
4379 {
4380 *msgp++ = ':';
4381 *msgp++ = ' ';
4382 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 }
4384 else
4385 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004386 len += (int)(STRLEN(message)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004387 + 2 // for the NL's
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004388 + STRLEN(buttons)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004389 + 3); // for the ": " and NUL
4390 lenhotkey++; // for the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391
Bram Moolenaar85a20022019-12-21 18:25:54 +01004392 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 if (!has_hotkey[0])
4394 {
4395 first_hotkey = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004396 len += 2; // "x" -> "[x]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 }
4398
4399 /*
4400 * Now allocate and load the strings
4401 */
4402 vim_free(confirm_msg);
4403 confirm_msg = alloc(len);
4404 if (confirm_msg == NULL)
4405 return NULL;
4406 *confirm_msg = NUL;
4407 hotk = alloc(lenhotkey);
4408 if (hotk == NULL)
4409 return NULL;
4410
4411 *confirm_msg = '\n';
4412 STRCPY(confirm_msg + 1, message);
4413
4414 msgp = confirm_msg + 1 + STRLEN(message);
4415 hotkp = hotk;
4416
Bram Moolenaar85a20022019-12-21 18:25:54 +01004417 // Define first default hotkey. Keep the hotkey string NUL
4418 // terminated to avoid reading past the end.
Bram Moolenaar6a516062007-07-05 08:11:42 +00004419 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420
Bram Moolenaar85a20022019-12-21 18:25:54 +01004421 // Remember where the choices start, displaying starts here when
4422 // "hotkp" typed at the more prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 confirm_msg_tail = msgp;
4424 *msgp++ = '\n';
4425 }
4426 }
4427
4428 display_confirm_msg();
4429 return hotk;
4430}
4431
4432/*
4433 * Display the ":confirm" message. Also called when screen resized.
4434 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004435 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004436display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004438 // avoid that 'q' at the more prompt truncates the message here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 ++confirm_msg_used;
4440 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004441 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 --confirm_msg_used;
4443}
4444
Bram Moolenaar85a20022019-12-21 18:25:54 +01004445#endif // FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446
4447#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4448
4449 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004450vim_dialog_yesno(
4451 int type,
4452 char_u *title,
4453 char_u *message,
4454 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455{
4456 if (do_dialog(type,
4457 title == NULL ? (char_u *)_("Question") : title,
4458 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004459 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 return VIM_YES;
4461 return VIM_NO;
4462}
4463
4464 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004465vim_dialog_yesnocancel(
4466 int type,
4467 char_u *title,
4468 char_u *message,
4469 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470{
4471 switch (do_dialog(type,
4472 title == NULL ? (char_u *)_("Question") : title,
4473 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004474 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 {
4476 case 1: return VIM_YES;
4477 case 2: return VIM_NO;
4478 }
4479 return VIM_CANCEL;
4480}
4481
4482 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004483vim_dialog_yesnoallcancel(
4484 int type,
4485 char_u *title,
4486 char_u *message,
4487 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488{
4489 switch (do_dialog(type,
4490 title == NULL ? (char_u *)"Question" : title,
4491 message,
4492 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004493 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 {
4495 case 1: return VIM_YES;
4496 case 2: return VIM_NO;
4497 case 3: return VIM_ALL;
4498 case 4: return VIM_DISCARDALL;
4499 }
4500 return VIM_CANCEL;
4501}
4502
Bram Moolenaar85a20022019-12-21 18:25:54 +01004503#endif // FEAT_GUI_DIALOG || FEAT_CON_DIALOG